MrSpock Posted January 19, 2014 Report Posted January 19, 2014 I am following the tutorial on Object Oriented PHP (which is something I've always struggled to grasp) and I'm using __construct() throughout the tutorial. Can you explain it in layman's terms as I'm struggle to understand it? For example: class employee extends person { function __construct($employee_name) { $this->set_name($employee_name); } } Why is the __construct() needed here? Many thanks Spock. Quote
falkencreative Posted January 19, 2014 Report Posted January 19, 2014 __contstruct() just allows you a way to perform various actions when the object is first created. Exactly what it does depends on what you need... but it could set various variables, connect to the database and retrieve data, etc. For example, take those two pieces of code: class employee extends person { function __construct($employee_name) { $this->set_name($employee_name); } } class employee extends person { function __construct() { } } The first would allow you to set the name of the employee when the object is first created: $person = new Employee('Their Name'); The second doesn't have that ability, so you'd need to have a set_name() method and call that separately. $person = new Employee(); $person->set_name('Their Name') Hopefully that makes sense? A more "real world" example might be something like this... say you ran a business, and were creating some software that stored information about your employees. For example, you could create an Employee object and pass in the employee ID when that object is first created: $employee = new Employee(1234); if you have a _construct set up, immediately when that object is created, you could run code to access the database and automatically populate information about that person (name, birthday, weekly schedule, etc.). You could do that same thing without __construct... but the code would be cleaner and simpler with __construct. The more simple and concise your code is, the easier it is to maintain and understand. Quote
MrSpock Posted January 19, 2014 Author Report Posted January 19, 2014 class person { var $name; public $height; protected $social_insurance; private $pinn_number; function __construct($persons_name) { $this->name = $persons_name; } function set_name($new_name){ $this->name = $new_name; } function get_name(){ return $this->name; } } class employee extends person{ function __construct($employee_name) { $this->set_name($employee_name); } } so to use the classes above, using __construct() allows access to set_name and get_name when I call a new object of employee? I'm still struggling to grasp it. Quote
falkencreative Posted January 20, 2014 Report Posted January 20, 2014 So in the classes above, __construct() in both Person and Employee allows you to set the name of the person or employee when you create the object. The __construct() is automatically called when the object is created. The __construct() doesn't have anything to do with allowing or not allowing access to set_name() or get_name(). It simply allows you to set a name when the object is created. Quote
Moloney Posted January 25, 2014 Report Posted January 25, 2014 (edited) Hi, I have a question also related to constructors in PHP. I studied some JAVA for 3 months and now I am starting to learn some c#. I noticed that in these languages, we can overload the constructor. Then, recently I went back to PHP to do some web development and I noticed I couldn't find a way to overload the constructor. I was just wondering what you do in PHP if you wanted to overload a constructor? Edited January 25, 2014 by Stephenius Quote
falkencreative Posted January 25, 2014 Report Posted January 25, 2014 The simple fact is, you can't. See http://stackoverflow.com/questions/7729700/constructor-overloading-in-php or http://codereview.stackexchange.com/questions/504/workaround-for-overloaded-constructor-in-php for comments/alternatives. Quote
Moloney Posted January 26, 2014 Report Posted January 26, 2014 The simple fact is, you can't. See http://stackoverflow...rloading-in-php or http://codereview.st...structor-in-php for comments/alternatives. Thanks Ben. The second post was useful. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.