New to PHP 5 Magic functions (in general) allow you to define class functionality without needing to duplicate code I say in general since some of them don’t really seem to follow that definition and I didn’t find a definition that covers them all.
__construct() If you’ve done any OOP in PHP5, you’ve probably run into this method. Used to initialize an object. Best practices say to make sure you don’t do work in the constructor. Just assign values … and remember Dependency Injection (coming up next)
__construct() Constructor is setting values, but not doing work
__construct() Doing work in constructors makes it harder to reuse the class.
__construct() If you don’t provide a constructor method, and extend a class, the parent constructor will be called automatically. If you do provide a constructor, and want the parent constructor called, you must called parent::__construct()
__construct() By making the constructor inaccessible, you can prevent external instantiation. For example, implementing the singleton pattern, we don’t want external instantiation since we cannot control how many objects are created.
__destruct() __destruct() is called when all references to an object are removed object is explicitly destroyed shutdown sequence is initiated
__destruct() Parent destructors must be called explicitly if destructor is provided
__destruct() You could use this method to ensure an object logs itself.
__destruct() The destructor is called even if a script exits due to a call to exit() However, if you call exit() in a destructor, the other destructors will be skipped. Throwing an exception in the destructor is a fatal error.
__toString() Called whenever you try to use an object in a string context
__toString() You can invoke via echo, print, *printf Cast to string $var = (string)$obj;
__toString() __toString() cannot throw an exception – results in a fatal error If you call functions or methods in your __toString() that can throw an exception, make sure you handle it within the __toString() method
__get() Called when code tries to access non-accessible properties
__get() Non-accessible can mean either the property is not defined or that it is not public If it’s not defined, then __get() will be called from both inside and outside of the class context
__clone() Used if custom clone() behavior is needed If clone() is called on an object that contains other objects, both the clone and the original contain the same objects. Define __clone() to define what happens when an object is cloned
__clone() Default Clone behavior Using === to show they are the same
__set_state() Why the differences? stdClass doesn’t have a __set_state() Since $val is protected, I need a setter. Plus it shows how to deal with non-public stuff
Brief Look at other magic methods You’ve already seen 48 slides, and chances are it’s getting close to an hour… So here’s a quick overview of a few more of the magic methods
__callStatic() New in PHP 5.3 Very similar to __call() Intended for when a method is called in a static context (inaccessible method) SomeClass::protectedStaticMethod();
__sleep() Called when an object is serialized with PHP’s serialize. Can be used to shutdown and remove resources (like database connections) which cannot be serialized
__wakeup() Called when an object is unserialized. Can be used to re-establish connections and re-initialize resources ie, reconnect to the database
__invoke() New in PHP 5.3 Allows you to treat an object like a function
__autoload() Everyone using PHP 5 should be using this, or at least some form of autoloading. Allows you to automatically load PHP files when they are needed. No more need for require_once or include_once in your scripts