Overloading

Changelog: 5.3.0 Added __callStatic(). Added warning to enforce public visibility and non-static declaration. 5.1.0 Added __isset() and __unset(). Added support for __get() for overloading of private properties. 5.0.0 Added __get(). Examples: Overloading properties via the __get(), __set(), __isset() and __unset() methods __set() is run when writing data to inaccessi

Object Iteration

Examples: Simple Object Iteration PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration. <?php class MyClass {     public $var1 = 'value 1';     public $var2 = 'value 2';     public $var3 = 'value 3';     protected $protected = 'protected var';     private   $private   = 'private var';     function iterateVisibl

Magic Methods

Examples: Sleep and wakeup serialize() checks if your class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued. Note: It is not possible for __sleep() to return names of private properties in

Final Keyword

Examples: Final methods example PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. <?php class BaseClass {    public function test() {        echo "BaseClass::test() called\n";    }        final public function moreTesting() {        echo "BaseClass::moreTesting() called\n";    } } class ChildClass extends BaseClas

Object Cloning

Examples: Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to

Comparing Objects

Examples: Example of object comparison in PHP 5 When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class. When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class. An example will clarify these rules. <?php function bool2str($bool

Type Hinting

Late Static Bindings

Examples: self:: usage Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined: <?php class A {     public static function who() {         echo __CLASS__;     }     public static function test() {         self::who();     } } class B extends A {     public static function who() {         echo __CLASS__;     } } B::test(); ?> The above example will output: A

Objects and references

Examples: References and Objects One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples. A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows o

Object Serialization

Examples: serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialize() can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object. The methods in an object will not be saved, only the name of the class. In order to be able to unserialize() an object, the class of that object needs to be defined. That is, if you have an object of cla