Page 1 of 1

what's the code meaning?

Posted: Fri Jul 16, 2010 1:58 am
by everydayrun

Code: Select all

<?php
class books {
//public
public $title = array();
public $image = array();
public $author = array();
public $description = array();
public $year = array ();
public $price = array();
// private
private $filename = "data.txt";
//class constructor
function __construct()
{
$i=-1;
$lines = file($this->filename);
foreach ( $lines as $line) {
if (strlen($line) > 2) {
$line = rtrim($line);
list($what, $content) = explode("=> ", $line);
if ($what == "Title") {
$i++;
$this->title[$i]=$content;
}
elseif ($what == "Image") {
$this->image[$i]=$content;
}
elseif ($what == "Author") {
$this->author[$i]=$content;
}
elseif ($what == "Description") {
$this->description[$i]=$content;
}
elseif ($what == "Year") {
$this->year[$i]=$content;
}
elseif ($what == "Price") {
$this->price[$i]=$content;
};
};
};
} // end constructor
} // end GetData
?>
cant' understand code whcih from //class constructor to the last. why it must be create a class constructor? thank you.

Re: what's the code meaning?

Posted: Fri Jul 16, 2010 5:01 am
by pbs
It is not required to create constructor function if there is not needed. Regarding this constructor function, it is used to read a file and initialize array variable in class.