- Overview
- Transcript
3.2 Magic Methods
You may think that all we have is the methods we see here, but that’s not entirely true. Actually, every class is full of “magic methods,” waiting to have their power unleashed. I’ll run you through some of them.
1.Welcome1 lesson, 01:07
1.1Welcome01:07
2.The Absolute Basics of OOP5 lessons, 22:53
2.1What Is OOP?04:40
2.2Classes vs Objects05:42
2.3Class Constants and Internal Reference04:19
2.4Public vs Private Scope06:05
2.5Copying vs Cloning Objects02:07
3.Digging a Little Deeper Into OOP5 lessons, 43:52
3.1The Single Responsibility Principle15:18
3.2Magic Methods11:16
3.3Autoloading Through SPL03:37
3.4Working With Namespaces05:14
3.5Autoloading With PSR-008:27
4.OOP inheritance2 lessons, 12:02
4.1Class Inheritance And Protected Scope09:22
4.2Overriding Parent Methods02:40
5.OOP Abstractions5 lessons, 1:00:05
5.1Abstract Classes05:59
5.2Interfaces14:53
5.3Static Properties And Methods06:49
5.4Traits14:44
5.5Dependency Injection17:40
3.2 Magic Methods
Hello, and welcome back to PHP OOP Fundamentals. Now in the previous lesson, we had a look at the single responsibility principle. In this video, we'll work with magic methods. First, let's have a look at our user class. Now you may think that all we have is the methods we see here. Well that's not entirely true. Actually this class, like every other class, is full of what is called magic methods waiting to have their power unleashed. Now magic methods are called invisibly, and as a result, we often forget that they exist. But they are there, make no mistake. And as powerful as magic methods are, they can be very confusing whenever used. So, document their use well, or you will hate yourself when you come back to your code next year. Remember, with great power, comes great responsibility. Now chances are you've worked with some magic methods already. You've probably come across this one before. It's called a constructor, and it's run every time an object is created from this class. And like any of the magic methods, you define the constructor as if you're creating it from scratch. While in your reality, you're simply hooking into that magic method. Now like any other magic method, the constructor is proceeded with two underscores, and if you set up a constructor in a class, you can use it to configure that class, because that's what it's for. Now let's see what that means. We could, for instance, make it possible to pass the email address and the password for the user in the constructor. Now to do that, let's make the constructor accept an array. And that would be an array of possible values, and let's just call them params. Now I think we should make the array optional, because I would hate to force myself to provide any configurations every time I instantiate a new user. But we will require it to be an array, and that's what this tied pin in here will take care of. Now the next thing we want to do is iterate over that array, but only if the array actually contains items. So we'll do a conditional and count the params inside of the array. And only if it contains any items, we'll run through them and say that for each params, as key value, we'll just set this key to the value of our variable value. Okay, now let's see if we can make that work. We'll just open up index.php. Here we have a data array, and we'll just pass that when we create a new user, like so. Now if all goes well, then this should actually create that new user. Let's check inside the browser and see, and yes indeed it has. So, a constructor is used to configure an object on instantiation. Now don't be tempted to make it do any actual word though, because that's not what it's for, and it would make your object impossible to test. Now that's just one magic method, but there are many, many more. Each one of them is called automatically and you can hook into them. Now let's just have a look at our email and password properties again. They're private, right? Which means that you cannot set them from the outside. We'll just open up index.php and try to do just that. We'll just call $joost->email = and then we'll just pass in some other email, @tutsplus.com. Okay now, in the browser you can see that you cannot access this private property. However, using magic methods, we can make it feel like we can do so. Meet magic method set. We'll hook into that magic method right here, set. Now this is called when trying to set an inaccessible property in an object. Now it can be inaccessible, either because it's declared private, or maybe it doesn't exist at all. Either way, when somebody tries to set an inaccessible property, this method here is run. It takes the name and the value of the property as it's parameters, and then inside of this method, you can do whatever you like. Let's just do the easiest thing here, and let's just set $this->$name = $value. And now when you return to the browser you see that our error is gone, and the email address has actually been set to someotheremail@tutsplus.com. But now let's see what happens if I try to set a nonexistent property, such as shoe size. We'll just set that to 43 here. Now when we return to the browser, you'll see that shoe size has actually been set. Now you my not want this to happen, so let's fix that. Let's go back to the user class and create a little if statement here. We'll just check to see if the property we're trying to set actually exists. So we'll just check for isset($this->$name) and only if the property exists, we'll set it. Let's just remove this, and now if we return to the browser, you can see that choose sides is no longer being set. And you will notice that this also holds good for the parameters that are being passed through the constructor. Let's go back to index.php and add a little parameter here. We'll call it foo, and that will be equal to a value of bar. Now remember, we are passing this entire data array when we create a new user, so we're also passing foo. But then if we check in the browser, foo is nowhere to be found, and that's because even inside of the class, if you try to set a nonexisting property, PHP will use the set method. Okay, so now we can make the user feel like he has actual access to our properties. But what if I want to enable only some of the properties to be set like this? Well in that case, I can define an array at the top of my class here. I'll make it private and call it fillible. Now that will be equal to an array, and I'll just fill it with all the property names that I want users to be able to set, like so. For now, let's say we only want the email property to be set through the set method, okay? Inside of the set method, let's do another conditional. We'll just check to see if the name is actually inside of this fillable array. Well if it is, we want to be able to set it, but if it's not let's just return false. So if the name of the property that I'm trying to set is not inside of the fillable array, this will never get cold. Let's go back to index.php, and try and make that work. We'll try and set password and we'll just make it equal to some random gibberish. Now let's just check to see what that gives us in the browser now. We are setting the password to an integer here. Well actually, it's a string containing an integer. And we are trying to override that with a string. Let's see if that happens. Nope, that's still an integer. Let's try something else. Let's go inside of our user class, and add the password property to our fillable array. And now? Oh yes, it's actually being set. Now the counterpart of the set method is called Git. I'll just define that here. Now the Git method is called when trying to get the value of an inaccessible property in an object. So without the Git method defined, we would get an error if we try to do something like this. var_dump($joost->email). Because, again, I am trying to access a private property which is not allowed. But then, if inside my user class I have this Git method defined, and I'll just save that so it is actually defined, then that error has gone, okay? Now let's make this method actually return something. We'll do a check and see if ($this->$name) is set, then we'll just return that. And if not, then we'll just return NULL. Okay, so back to the browser. Here's the dump statement from our index.php. Now, again, as with the set function, you may not want the user to have access to all properties. Now let's create a solution that's very similar to the fillable array solution that we had before. We'll just call this accessible, and let's just say we only want the email property to be accessible like this. Let's scroll down just a little, and copy this bit of code here from the set method, and paste it in here. And right here, we'll check to see if the name property is in the accessible array, and then if it's not, we'll not return false but we'll return null. So if we go back to index.php and we'll not only try and var_dump($joost->$email), but also password. Now this should get us the email, and this should get us NULL. Let's see if that happens. There you go, there's our email, and there's NULL, just like we expected. Okay, now the last widely used magic method I would like to discuss with you is the ToString method. Let's just define that here, ToString. Now this is called when an object is cast to a string, so for instance if I were to do something like echo $joost. Now normally in the index.php, this would throw an error because $joost is an object there. But we can make the ToString actually return something and then that will be echoed here. Maybe that would be a JSON object. Something like that. Actually that would be quite nice. Let's just create a data array and we'll just use that to construct our JSON object. So, add then the other mehod, we'll just return json_encode, and then we'll just pass in the data array, like so. Now in order to fill our data array, let's just loop through the accessible array, because that holds the properties that we want to be accessed publicly. So we'll just do a through each statement, and say for each this accessible, ask key value. And then we'll just take the data array, add our key, and make it equal to value. Now let's see if that's worked. All we need to do to check that is echo $joost. So let's save this out, return to index.php, and just comment out some of these dumps, like so. And what we'll do here at the bottom is just echo $joost. And as you can see, we're getting an email key but no email value, and that means I've done something wrong here. Oh yes, it's because I'm using this accessible in a wrong way. Let's just do for this accessible as key, and then just fill data[$key] with $this->key because that's the variable that we're actually trying to get the value from. Now let's just reload that and there you go. There's our JSON object containing email. And again, if we add passwords to our accessible array as well, like so, then that should be inside of our JSON object as well, and it is. Okay, now in this video, we've gone through a number of magic methods, but there are many. For a complete list of magic methods, consult the manual pages of www.php.net. And let's just consider that to be your homework for this lesson. Well, that's all for magic methods. I'll see you in the next video, which will be about autoloading classes. I'll see you there.







