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

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

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

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

Class Constants

Examples: Defining and using a constant It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the $ symbol to declare or use them. The value must be a constant expression, not (for example) a variable, a property, or a function call. It's also possible for interfaces to have constants. Look at the interface documentation for examples. As of PHP 5.3.0, it's po

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

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

Properties

Examples: property declarations Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able

Internal (built-in) functions