0

i have been reading about magic functions lately, and I'm really confused about their implementations. Some Magic functions such as __contruct() and __destruct() are very useful. Magic functions like __construct() could be used to initialize variables with default values.

However I'm really confused with the implementation of other magic functions such as __isset(), __call(), __toString(), etc. What is the actual purpose of implementing the Magic Functions.

Yes, I do understand they are invoked behind the scenes and do not require a function call, but then what is their main advantage in real world, in terms of security-sql injection-scope. The main difference between isset() and __isset() (or any other Magic Function) and the situations in which I should use them?

11
  • 4
    Well look at magic methods
    – B001ᛦ
    Jul 19, 2016 at 13:27
  • __toString() is when you do echo $class repl.it/Cd6w/1
    – ʰᵈˑ
    Jul 19, 2016 at 13:28
  • @bub Yes I did and I still don't find its use in real world. Can you please explain instead of posting links? Some examples and explanation would be good. Jul 19, 2016 at 13:28
  • 1
    That's precisely what you do - for instance, you can add a __get($var) magic method to your class. That can retrieve, sanitise and return the relevant data from the class member (private or otherwise).
    – CD001
    Jul 19, 2016 at 13:56
  • 1
    In a nutshell: it allows you simplify syntax in certain circumstances. That's pretty much all. E.g. it allows you to write $myObj->foo instead of $myObj->getFoo() while still retaining the possibilities of a function call. If you extend "magic methods" to include ArrayAccess and Iterable it allows you some polymorphic possibilities like $myObj['foo'] and foreach ($myObj as ...).
    – deceze
    Jul 19, 2016 at 14:07

1 Answer 1

6

You shouldn't use magic methods in PHP unless you want some documented magic functionality.

They allow you to react to certain events when using these particular objects. This means when certain things happen to your object, you can define how it should react in that instance.

Each of these methods are triggered automatically and you are just defining what should happen under these circumstances.

Probably you won't ever need to use any of them besides __construct() and __destruct() when dealing with objects.

__construct() - Is called when an object is first created. You can inject parameters and dependencies to set your object up.

__destruct() - Is called when an object is destroyed. You can write some cleanup code here. Closing any open datastreams, database connections... whatever.

__get() - Listens for get requests of the properties.

__set() - Listens for set requests of the properties.

__isset() - Triggered by calling isset() or empty() on the object's properties.

__unset() - Triggered by calling unset() on the object's properties.

__toString() - Allows you to define how an object will behave when it's treated like a string.

__sleep() - Code defined here will run before you use serialize(). So you can define which properties of the object should be serialised.

__wakeup() - This is used to reinitialize any tasks that may have been put to stop during the serialization.

__invoke() - Defines how your class should behave if you "invoke" it and use it like a function.

__clone() - Triggered when cloning an object is finished. (If you copy your objects they are still linked to their original as they are still referencing it. Cloning can get you a clean copy.)

__debugInfo - Triggered when using var_dump() on the object. You can use it to control what kind of info should show up in the dump. If the method isn't defined on an object, then all public, protected and private properties will be shown.

3
  • So suppose for example, isset() is used to check if a variable/object exist. If i write my own __isset(), would i be able to change the default behavior of isset()?? Jul 19, 2016 at 13:41
  • kinda like operator overloading in C++?? Jul 19, 2016 at 13:42
  • Yes. __isset() captures the result of isset() and you can manipulate the output as you see fit.
    – PockeTiger
    Jul 19, 2016 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.