Type Hinting

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

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

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

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

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

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

Anonymous (class)es

Examples: Support for anonymous classes was added in PHP 7. Anonymous classes are useful when simple, one-off objects need to be created. <?php // Pre PHP 7 code class Logger {     public function log($msg)     {         echo $msg;     } } $util->setLogger(new Logger()); // PHP 7+ code $util->setLogger(new class {     public function log($msg)     {         echo $msg;     } }); The above example will output: They can pass argu

Traits

Examples: Trait example As of PHP 5.4.0, PHP implements a method of code reuse called Traits. Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity

Object Interfaces

Examples: Interface example <?php // Declare the interface 'iTemplate' interface iTemplate {     public function setVariable($name, $var);     public function getHtml($template); } // Implement the interface // This will work class Template implements iTemplate {     private $vars = array();        public function setVariable($name, $var)     {         $this->vars[$name] = $var;     }        public function getHtml($template)     {         foreach($this->va