Autoloading Classes

Examples: Autoload example Note: Prior to PHP 5.3, exceptions thrown in the __autoload() function could not be caught in the catch block and would result in a fatal error. From PHP 5.3 and upwards, this is possible provided that if a custom exception is thrown, then the custom exception class is available. The __autoload() function may be used recursively to autoload the custom exception class. Note: Autoloading is not available if using PHP in CLI interactive m

Constructors and Destructors

Examples: using new unified constructors PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child construct

Visibility

Examples: Property declaration Class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public. <?php /**  * Define MyClass  */ class MyClass {     public $public = 'Public';     protected $protected = 'Protected';     private $private = 'Private';     function printHello()     {         echo $this->public;         echo $this->protected;         echo $this->private;     } } $obj = new MyC

Object Inheritance

Examples: Inheritance Example <?php class Foo {     public function printItem($string)     {         echo 'Foo: ' . $string . PHP_EOL;     }          public function printPHP()     {         echo 'PHP is great.' . PHP_EOL;     } } class Bar extends Foo {     public function printItem($string)     {         echo 'Bar: ' . $string . PHP_EOL;     } } $foo = new Foo(); $bar = new Bar(); $foo->printItem('baz'); // Output: 'Foo: baz' $foo->printPHP();       // O

Scope Resolution Operator (::)

Examples: :: from outside the class definition The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class. When referencing these items from outside the class definition, use the name of the class. As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, p

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

Class Abstraction

Examples: Abstract class example PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these

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

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

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