runeveryday Posted July 16, 2010 Share Posted July 16, 2010 <?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 More sharing options...
Mchl Posted July 16, 2010 Share Posted July 16, 2010 constructor is a function that's being called when new instance of this class object is created. So when you do $booksObj = new books() the code in __construct() is executed Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086916 Share on other sites More sharing options...
runeveryday Posted July 16, 2010 Author Share Posted July 16, 2010 as i know,in class, i can declare function optionally.but why i shoud declare a function like this"function __construct(){ }". Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086935 Share on other sites More sharing options...
Mchl Posted July 16, 2010 Share Posted July 16, 2010 Because that's the way to declare constructors in PHP Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086939 Share on other sites More sharing options...
runeveryday Posted July 16, 2010 Author Share Posted July 16, 2010 Because that's the way to declare constructors in PHP i know,but don't know when i should declare constructors in a class and what's the role it plays in a class. thank you. Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086944 Share on other sites More sharing options...
Mchl Posted July 16, 2010 Share Posted July 16, 2010 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. Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086948 Share on other sites More sharing options...
runeveryday Posted July 16, 2010 Author Share Posted July 16, 2010 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. Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086955 Share on other sites More sharing options...
Mchl Posted July 16, 2010 Share Posted July 16, 2010 But then you need to call this function explicitly, while __construct() is called automatically. Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086957 Share on other sites More sharing options...
runeveryday Posted July 16, 2010 Author Share Posted July 16, 2010 But then you need to call this function explicitly, while __construct() is called automatically. got it, many thanks Link to comment https://forums.phpfreaks.com/topic/207917-whats-the-code-meaning/#findComment-1086960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.