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

Type Hinting

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

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

Static Keyword

Examples: Static method example <?php class Foo {     public static function aStaticMethod() {         // ...     } } Foo::aStaticMethod(); $classname = 'Foo'; $classname::aStaticMethod(); // As of PHP 5.3.0 ?> Static property example Static properties cannot be accessed through the object using the arrow operator ->. Like any other PHP static variable, static properties may only be initialized using a literal or constant before

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