Join Kevin Skoglund for an in-depth discussion in this video Constructor arguments, part of PHP: Object-Oriented Programming.
- [Instructor] Let's continue our discussion of constructor methods by talking about their arguments. Constructor methods are just methods, and methods are really just functions inside an object. So it makes sense that we are also able to pass in arguments to them, just like we could to any PHP function. You can see here, after the construct method name, I have parentheses, and I have two arguments; one for name and one for color. Now when I create a new object, in order to pass in those arguments, you just add the parentheses after the class name, and it will send in your arguments.
So when I create a new instance of product, I've got parentheses and two values are being passed in to those arguments; one for T-shirt and one for blue. Now the result is that my shirt color is still blue, but it's not hard-coded as a default value, and it's not hard-coded in my constructor method. I'm passing it in as an argument when I create an instance of this object. It's common that you want to initialize an object with certain properties when you first create it. One big advantage of doing it this way is I can pass in values to properties that are otherwise private.
I don't have to create a new instance, and then set the values from outside the class. I can pass it into the constructor method, and it can set them from inside the class where it has access to private properties. Now, all the normal rules about function arguments apply. For example, if I have two arguments listed like this, then I must provide exactly two values, not fewer or more. We can, of course, set default values for each argument, like this. But there are still a few drawbacks to listing out each and every argument. Even with these default values, we aren't able to pass in the color argument, without putting some kind of a name argument first.
The order of the arguments can be hard to remember, too. The argument list can become very long, especially for an object where we may be initializing a lot of different properties. This code can also be hard to read, because when we pass in the values? That's unlabeled data and it can be difficult to tell what argument it's referring to. Here, it may seem obvious that I'm passing in the name and the color. But what if I had several values that were just numbers? It becomes harder to know which is which, and if I've got them in the right order. A common technique is to make arguments into a single array, so that it's less sensitive to these kinds of things.
Here, I'm also using the null-coalescing operator, added in PHP 7, so that it gracefully defaults to the value that I've got after the double question mark, if an array key is missing. The same thing is checking to see if the key is set before trying to use it. Doing it this way has several advantages. The argument list is short, the data is clearly labeled, so that I know what each one of those values belongs to, the order of the values in the array doesn't matter, and the presence or absence of data in the array doesn't matter, either.
Now this technique of using an array instead of a list of arguments can be applied to all functions in PHP. But is especially useful when working with objects and the construct method. Let's continue working with our constructors file that we working with on the last movie, and let's start out by just adding a couple of arguments here. One for seats and one for arms. And what I'm going to do here is, after I've incremented the instance count, Let's set this seats equal to whatever value is passed in for seats.
And this arms can be equal to whatever value is passed in for arms. Notice that we can tell the difference between the argument local variable that we're using inside the function and the object property which begins with this, and the arrow notation before it. Now that I have that set, I have the ability to pass in values here. So I have sofa, and then I have a set of parentheses after it where I can pass in two values. So for the first one, let's put in three comma two. For the couch, we'll put in three comma one.
We'll say that this couch actually has one arm on it, and for the loveseat we're going to pass in two seats and two arms. Notice already it's a little tricky to remember which order these are in. It's very easy to reverse these and accidentally put one before the three. In order to know which is which, we have to refer back to our method to check the order of the arguments. So let's save this and let's go back over here and let's try it. Now, I changed it so that couch should have one arm. Let's reload the page, and sure enough, it has one arm on the couch now.
Everything else stayed the same as the default values. Now you can keep these default values in there, if you want, or you can remove them, it's up to you. But notice that I cannot remove this value here. I cannot say well a loveseat, let's just take this value away. Come over here and reload the page, I get an error because the argument count is wrong, right? I have to have arms, and even if I put in, Well, by default let's make the arms two, and let's make the seats equal to three, right? Now it won't complain any more.
If I do this, reload our page. Now, it works fine but what is this number here, right? What if I wanted to pass in only a change to the number of arms, and I didn't want to change this? I could pass in null, come over here and reload it. That's not what I wanted, right? I wanted the default value that it already had. So you can see why that's not ideal. A better solution, a better solution is to implement that pattern that we just talked about. Where instead of passing in individual arguments, we're going to pass in an array of arguments.
There'll be no empty array if nothing is set, and then we'll check here inside args to see if the seats key is set. Then we'll use it and we'll use that null-coalescing operator from PHP 7 and default to null, if it's not. You can set it to something else as a default, if you wanted. You could put this equal to three as a default. I'm just going to make it null, and I'm just going to copy that, so I don't have to retype it all.
But this one, the key is going to be arms. Now, I can substitute this out, and instead I'll pass in an array where it says seats are going to be three, and arms are going to be two. I'll just copy that array, so I can paste it in here, for couch and for loveseat. Now, I can easily remove some of these, if I want. Let's say that the couch, this is a couch that has four seats in it.
Sort of an oddball couch. And let's say that our loveseat is only going to have no arms. Normally it defaults to having two, because it inherits that. So let's change that, let's come back over and reload our page. Oops, I have an error here. Line 26, so let's go fix that error. Ah, I put equals and that needed to be an arrow notation. There we go, let's try it again. There we go. Now, arms and seats are being set to null by default.
So probably what I want to do is come over and set those to some default value, which is like this. I'll just use whatever value it already has. Let's come back and save that. Now that's going to be arms. I'm going to reload the page and now those defaults are there. So use the new value if you have it, otherwise, fall back to the default. So the main points I want you to get out of this are first, that you can pass in arguments to the constructor, and that the way you do that is putting parentheses after the class name.
And the last point is that it may be beneficial for you to use, look at this arguments as an array design pattern, instead of trying to list out all the individual arguments.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project