A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearBenefits of using a constructor? - page 1
User InfoPosts
Benefits of using a constructor?#1
In my quest in trying to learn more about OOP in PHP. I have come across the constructor function a good few times and simply can(t ignore it anymore. In my understanding, the constructor is called upon the moment I create an object, is this correct?

But why would I need to create this constructor if I can use "normal" functions or methods as their called?

cheers,
Keith

posted date: 2008-12-31 10:44:00


Re: Benefits of using a constructor?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-31 10:44:01


Re: Benefits of using a constructor?#3
Yes the constructor is called when the object is created.A small example of the usefulness of a constructor is thisclass Bar{ // The variable we will be using within our class var $val; // This function is called when someone does $foo = new Bar(); // But this constructor has also an $var within its definition, // So you have to do $foo = new Bar("some data") function __construct($var) { // Assign(s the $var from the constructor to the $val variable // we defined above $this->val = $var }}$foo = new Bar("baz");echo $foo->val // baz// You can also do this to see everything defined within the classprint_r($foo);UPDATE:A question also asked why this should be used, a real life example is a database class, where you call the object with the username and password and table to connect to, which the constructor would connect to. Then you have the functions to do all the work within that database.

posted date: 2008-12-31 10:47:00


Re: Benefits of using a constructor?#4
Thanks for the reply Ólafur. If possible, could you comment each line for me to better understand? K

posted date: 2008-12-31 10:54:00


Re: Benefits of using a constructor?#5
The constructor allows you to ensure that the object is put in a particular state before you attempt to use it. For example, if your object has certain properties that are required for it to be used, you could initialize them in the constructor. Also, constructors allow a efficient way to initialize objects.

posted date: 2008-12-31 10:56:00


Re: Benefits of using a constructor?#6
Thanks Brian, perfect explanation!

posted date: 2008-12-31 10:57:00


Re: Benefits of using a constructor?#7
+1 for mentioning state

posted date: 2008-12-31 10:58:00


Re: Benefits of using a constructor?#8
The constructor is for initialisation done when an object is created. You would not want to call an arbitrary method on a newly created object because this goes against the idea of encapsulation, and would require code using this object to have inherent knowledge of its inner workings (and requires more effort).

posted date: 2010-12-27 18:58:00


Re: Benefits of using a constructor?#9
Also, see www.momige.com/403756/… which asks the same question

posted date: 2010-12-27 19:00:00


Re: Benefits of using a constructor?#10
ha ha, that's hilarious. That's my question I completely forgot about. I was looking in the Related Questions when asking my question and didn't see anything. Apologies as I can't delete this question as there are too many answers.

posted date: 2010-12-27 19:07:00


Re: Benefits of using a constructor?#11
2 years left, nothing chaged )))

posted date: 2010-12-27 19:13:00


Re: Benefits of using a constructor?#12
+1 for "required before work". That's what a constructor is for.

posted date: 2010-12-27 19:18:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.