Static members
Classes definitions can now include static members (properties), accessible via the class. Common usage of static members is in the Singleton pattern.
class Singleton {
static private $instance = NULL;
private function __construct() {
}
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new Singleton();
}
return self::$instance;
}
}
Static methods
You can now define methods as static allowing them to be called from non-object context. Static methods don't define the $this variable as they aren't bound to any specific object.
<?php
class MyClass {
static function helloWorld() {
print "Hello, world";
}
}
MyClass::helloWorld();
?>
abstract classes
A class may be declared as abstract so as to prevent it from being instantiated. However, you may inherit from an abstract class.
abstract class MyBaseClass {
function display() {
print "Default display routine being called";
}
}
abstract methods
A method may be declared as abstract, thereby deferring its definition to an inheriting class. A class that includes abstract methods must be declared as abstract.
abstract class MyBaseClass {
abstract function display();
}
Class type hints
Function declarations may include class type hints for their parameters. If the functions are called with an incorrect class type an error occurs.
function expectsMyClass(MyClass $obj) {
}