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

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

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

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

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

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

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

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

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

Basics

Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class. The class name can be any valid label, provided it is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$.