Official References (From the online PHP Manual):
- Classes and Objects (PHP 5): http://www.php.net/manual/en/language.oop5.php
- Type Operators: http://www.php.net/manual/en/language.operators.type.php
- Class/Object Functions: http://www.php.net/manual/en/book.classobj.php
Basic
<?php
// No methods in an interface are defined
interface ClassInterface {
// All methods in an interface must be public
public function method_normal($arg);
}
// Any class that contains one abstract method must also be abstract
// A class can implement at most one interface
abstract class ClassAbstract implements ClassInterface {
/* Member Declaration */
/*--------------------*/
// Initializer must be a constant expression
public $var_public = 1; // Visible publicly
protected $var_protected; // Visible by inherited classes
private $var_private; // Visible within this class
// Protected/Private members are accessible in different instances
// of the same class
// Static members are accessible without instantiation of the class
// Static members cannot be accessed with an instantiated class object
// Static members/methods cannot be accessed through -> operator
public static $var_static = 'static value';
// A constant do not use $ symbol
const var_constant = 'constant value';
/* Constructor */
/*-------------*/
public function __construct($arg1, $arg2='default')
{
// Called for each newly-created object
// Prevents direct object creation if declared private
}
/* Destructor */
/*------------*/
public function __destruct()
{
// Called implicitly if all references to this object are removed
}
/* Methods */
/*---------*/
public function __clone()
{
// Called in the new object cloned allowing changing of properties
$this->var_public++;
}
/* Abstract method only declares the method's signature */
abstract protected function method_one($arg);
// Member/Method assumed public if not explicitly specified
function method_normal($arg)
{
if (isset($this)) {
// $this refers to the calling object
}
else {
// Not defined if called statically
}
echo __CLASS__;
}
public static function method_static()
{
// Static methods are callable without instantiation of the class
// Calling non-static methods statically results in E_STRICT level warning
}
final protected function method_protected()
{
// Final methods cannot be overridden by inherited class
}
}
// Final class cannot be inherited
// A class can inherit at most one base class
final class ClassFinal extends ClassAbstract {
public $var_one = 'my default value';
// Can redeclare parent's public and protected variables/methods
protected $var_protected = 'my protected value';
public function __construct($arg1)
{
// Parent constructors are NOT called implicitly
parent::__construct($arg1);
}
public function __destruct()
{
// Parent destructors are NOT called implicitly
parent::__destruct();
}
// Define inherited abstract method with the same or less restricted visibility
public function method_one($arg)
{
parent::method_protected();
}
// Override the parent's method_normal();
public function method_normal($arg)
{
// Still can access parent's methods and members overridden
parent::method_one($arg);
// Miscellaneous
$a = new self('self');
$b = new parent('parent');
echo parent::var_constant."\n";
echo parent::$var_static."\n";
}
}
// The special __autoload() function is invoked when you
// try to use a class/interface which has not been defined yet.
function __autoload($class_name)
{
require_once "include/{$class_name}.php";
}
$object = new ClassFinal('something'); // Create an object
$object->method_one('arg'); // Call a method
$object_two = clone $object; // Clone an object
if ($object instanceof ClassAbstract) {
// True if the object is
// - an instance of a class
// - an instance of a class that inherits from a parent class
// - an instance of a class that implements an interface
}
foreach ($object as $member => $value) {
// Traverse each visible member
// Can also implement PHP's internal interfaces Iterator
// or IteratorAggregate to customize the iteration.
}
if ($object == $object_two) {
// True iff instances of same class, same attributes, and same values.
}
if ($object === $object_two) {
// True iff refer to the same instance of the same class
}
function demo_type_hinting(ClassFinal $obj)
{
// Force a function to only accept an object or the specified class
// NULL is accepted iff it is used as default paramater value
}
Also check the Class/Object Functions.
Advanced
<?php
class ClassAdvanced {
/* Overloading */
// PHP's Overloading (whose interpretation is different than most
// other OO languages) provides means to dynamically create
// members and methods.
// Overloading methods interact with inaccessible members/methods,
// which have not been declared or invisible in the current scope.
public function __set($name, $value)
{
// Triggered by writing data to inaccessible members
}
public function __get($name)
{
// Triggered by reading data from inaccessible members
return $value;
}
public function __isset($name)
{
// Triggered by calling isset() or empty() on inaccessible members
}
public function __unset($name)
{
// Triggered by calling unset() on inaccessible members
}
public function __call($name, $args)
{
// Triggered by calling inaccessible methods in object context
}
public function __callStatic($name, $args)
{
// Triggered by calling inaccessible methods in static context
}
/* Serialization */
public function __sleep()
{
// Triggered by caling serialize() prior to serialization
// Must return an array with member names that should be serialized
return array();
}
public function __wakeup()
{
// Triggered by calling unserialize()
}
/* String Conversion */
public function __toString()
{
// Returns the string when converted to a string
return 'string for this object';
}
/* __set_state */
public static function __set_state(array $array)
{
// Triggered by calling var_export()
// $array contains exported properties: array('property' => value,...)
// Can create parseable code when var_export() is used with objects
}
}
Other features not covered here:
- Reflection
- Late Static Binding

