Jump to content

what's the code meaning?


runeveryday

Recommended Posts

<?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.

Link to comment
https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/
Share on other sites

As I said before, a constructor is a function that is executed where an instance of the class is created. You should define it whenever you want some actions to be performed upon object instantiation.

but i also can declare a function,eg:function aa { } when i want some actions to be performed upon object instantiation. thank you.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.