6

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

3
  • Also, see stackoverflow.com/questions/403756/… which asks the same question Commented Dec 28, 2010 at 3:00
  • 1
    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. Commented Dec 28, 2010 at 3:07
  • 2 years left, nothing chaged ))) Commented Dec 28, 2010 at 3:13

5 Answers 5

28

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.

Sign up to request clarification or add additional context in comments.

Comments

10

Yes the constructor is called when the object is created.

A small example of the usefulness of a constructor is this

class 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 class print_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.

2 Comments

Thanks for the reply Ólafur. If possible, could you comment each line for me to better understand? K
Thank you for your explanation , but unable to understand your real life example , can you please explain more about it ?? thank you in advance.
6

The idea of constructor is to prepare initial bunch of data for the object, so it can behave expectedly.

Just call a method is not a deal, because you can forget to do that, and this cannot be specified as "required before work" in syntax - so you'll get "broken" object.

2 Comments

@Keith: To quote Wikipedia (and I usually don't, but it is well written in that case): "[The constructors] have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a valid state."
@netcoder: yep, I looked there right after I had written my answer - to test myself ;-) Since there is no significant difference and since SO is a site where people answer (not just copy-paste) - I'll leave mine, even though it is written with worse english ;-)
2

Constructors are good for a variety of things. They initialize variables in your class. Say you are creating a BankAccount class. $b = new BankAccount(60); has a constructor that gives the bank account an initial value. They set variables within the class basically or they can also initialize other classes (inheritance).

Comments

2

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

2 Comments

calling a setter method, per example, after the object has been created do not violate encapsulation at all. The constructor is used to define properties/arguments that are required by the object to behave as expected.
@netcoder True, but if the constructor involved, say, connecting to a database, you wouldn't necessarily expect the calling code to have to call a method to do this, as you might not even want the calling code to know where the object gets its info from. Good point, though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.