11.
Magic Methods: __toString() __get, __set()
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
7 Comments
Hi. I know these are rather old courses, still, I think someone messed with the challenge files, as a lot are broken.
I think it would be great to not include these coding challenges and instead replace them with questions as other challenges are, so no one gets to mess with them and the courses can be completed.
Hey @D-Marti
I agree with you, and actually, the team already decided to replace the coding challenges. It will happen soon
Sorry for the inconvenience.
Hey @D-Marti
I just want to inform you that we have removed the coding challenges (we'll add questions soon), so you should be able to finish the course without hassle.
Cheers!
Hey!
I get confused with magic methods. I don't get the logic behind it, for example how does $propertyName equels strength I get the __construct() but the rest is confusing.
Hey Emin,
About magic get() method: if your class has get() method - every time you when you call a property that does not exist or does not public - before throwing an error, PHP interpreter will call that __get() method and pass a property name to it that you're trying to access. So, if you have this code in your class:
Every time you call any property, for example, "$object->test" - the __get() method will be called and "test" string (the property name you're trying to call) will be passed as an argument, i.e. $propertyName variable will be equal to "test". And then, in that method, we call "return $this->$propertyName;" but since $propertyName = "test" it means you just call "return $this->test;" - i.e. you call property name directly.
So, basically, you need to understand the difference between "$this->propertyName;" and "$this->$propertyName;" - that's very important and I bet you confused because of it. So, you always call properties like "$this->propertyName;" inside your class, but "$this->$propertyName;" is also a legitimate syntax that means PHP will interpret the $propertyName variable first and then call a property that equal to that variable's value, for example:
I hope it clear for you now.
Cheers!
Hey victor,
Thank you for the fast reply. Now It makes more sense on how it works and with experience I will understand it allot better.
Cheers!
Hey Emin,
Yeah, sure! As always, you just need more practice. Glad it helped you
Cheers!
"Houston: no signs of life"
Start the conversation!