what's the code meaning?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

what's the code meaning?

Post 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.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: what's the code meaning?

Post 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.
Post Reply