From the course: PHP: Object-Oriented Programming

Constructor method

- [Instructor] In this chapter, we're going to be talking about a number of different magic methods that are built into PHP. We'll begin by talking about what's probably the most important and most commonly used, which is the constructor method. Magic methods are really not that magic. What they are are methods on PHP objects that get called automatically in some circumstances. And because it's automatic, it seems like it's magic. The ones that we're going to be focused on are construct, destruct and clone, but there are probably about a half a dozen more that are on the php.net website if you want to learn about them. A lot of them are more advanced or rarely used. We've actually bumped into a couple of these already when we talked about the problems that overloading causes us. PHP has magic methods for when we try to get or set a property that doesn't exist. The problem we had was that that magic method set by default is adding the property and setting its value for us. Each one of these magic method names has a double underscore in front of it. That can be a little hard to see, but it's underscore underscore and then the name of the method. And that's how we know that it's one of these magic methods is that double underscore. The way that we use a magic method is simply to define it as a method inside our class. Here you can see I have a method for underscore underscore construct. Magic methods must always be marked public. So you can see I have the public visibility modifier in front of it. Then inside the method, we can put whatever code we want. This code is calling another class and telling its static method log to log something to a log file. And then you can see that it's setting the property color equal to the string blue. Now whenever we create a new instance, this construct method will be automatically called. So at the bottom, you can see when I create a new instance of the product, the shirt automatically has that value of blue. It works like having a default value, but it's not a default value, it's being set up in this construct method. It's super useful. If you did the challenge assignment at the end of the last chapter, you wrote a static method called create, which created an instance and then incremented a static property for the instance count. It worked a bit like an instance factory that allowed us to do other work besides just creating the instance. You can see here in this side-by-side comparison how it compares to construct. Construct allows us to add this other work directly to the new call. And with construct, there's no need to return a value like we did in create. What's returned is always going to be an instance of an object. So when we call new in PHP, what's going on is the same thing is what our create method does. First, it creates a new instance of the called class. Second, it executes any custom code that we have in the construct method. And third, it returns the instance. So you can think of a construct method as a place to hold code that should be performed in step two. After an instance is created, but before it's returned. Let's try it out. Inside the exercise files, I've already created a file called constructors.php and you can find it there. It just has a class for sofa and then I have a class for couch and loveseat that extends sofa and I've just varied the number of default seats and arms that are going to be set there. And then, I've also added this public static instance count property and initialized it to zero. So for beginning with, that's all it does. It just outputs back the values for creating a new sofa and outputting its seat in arms, the couch, its seats and arms, and the loveseat and it seats and arms, and then finally, we'll just get a count of how many have been created. So let's save this, come back over here to our browser and you can see, I have already loaded in this page. I'll just reload it. The sofa has three seats and two arms. The couch has three seats and no arms. Loveseat has two seats and two arms. And the instance count is zero. And the reason the count is zero is because I didn't do anything that would increment that. So now let's add that construct method. So right here, I'll make a public function. Must be public. If we don't put anything in front of it, of course, it would be public by default, but it's a good habit to always put it there. Construct. And then, what do we want it to do inside of here? We want to do self colon colon dollar sign instance underscore count plus plus. Alright, let's save it. And now let's go back and let's reload our page. Instance count is equal to three. See how that works. Every time I called new, it created a new instance of the object. Then it called the construct magic method and then it returned the object to me so I could store it inside this variable sofa. And notice also that it was inherited as we would expect into each of our subclasses and they added one to the count as well.

Contents