Object Model
Object model is brand spanking new!
Private
Private and Protected member variables are now available
<?php
class MyClass {
private $Hello = "Hello, World!\n";
protected $Bar = "Hello, Foo!\n";
protected $Foo = "Hello, Bar!\n";
function printHello() {
print "MyClass::printHello() ".$this->Hello;
print "MyClass::printHello() ".$this->Bar;
print "MyClass::printHello() ".$this->Foo;
}
}
class MyClass2 extends MyClass {
protected $Foo;
function printHello() {
MyClass::printHello();
print "MyClass2::printHello() ".$this->Hello;
print "MyClass2::printHello() ".$this->Bar;
print "MyClass2::printHello() ".$this->Foo;
}
}
$obj = new MyClass();
print $obj->Hello; /* Shouldn't print out anything */
print $obj->Bar; /* Shouldn't print out anything */
print $obj->Foo; /* Shouldn't print out anything */
$obj->printHello(); /* Should print */
$obj = new MyClass2();
print $obj->Hello; /* Shouldn't print out anything */
print $obj->Bar; /* Shouldn't print out anything */
print $obj->Foo; /* Shouldn't print out anything */
$obj->printHello();
?>
Cloning
You are able to make direct copies of objects with PHPv5, furthermore
you can define functions that are called when the objects are copied.
<?php
class MyCloneable {
static $id = 0;
function MyCloneable() {
$this->id = self::$id++;
}
function __clone() {
$this->name = $clone->name;
$this->address = 'New York';
$this->id = self::$id++;
}
}
$obj = new MyCloneable();
$obj->name = 'Hello';
$obj->address = 'Tel-Aviv';
print $obj->id . "\n";
$obj = $obj->__clone();
print $obj->id . "\n";
print $obj->name . "\n";
print $obj->address . "\n";
?>
Nested Classes
In PHPv5 you can have classes within classes. This allows for many
neat features, most importantly, classes can function as namespaces.
<?php
class DB::MySQL {
var $host = '';
function db_connect($user) {
print "Connecting to MySQL database '$this->host' as $user\n";
}
}
class DB::Oracle {
var $host = 'localhost';
function db_connect($user) {
print "Connecting to Oracle database '$this->host' as $user\n";
}
}
$MySQL_obj = new DB::MySQL();
$MySQL_obj->db_connect('Susan');
$Oracle_obj = new DB::Oracle();
$Oracle_obj->db_connect('Barbara');
?>
Constants
Classes may now contain constants
<?php
class foo {
const hey = 'hello';
}
print foo::hey;
?>
Importing
You may now import class symbols into the current "namespace"
<?php
class MyClass {
class MyClass2 {
function hello() {
print "Hello, World in MyClass2\n";
}
}
function hello() {
print "Hello, World\n";
}
}
import function hello, class MyClass2 from MyClass;
MyClass2::hello();
hello();
?>
Unified Constructors and Destructors
The new OO model will allow for consistency amongst constructors and
destructors, so they are no longer dependent on they class name (PHPv4
actually only had the concept of constructors.)
<?php
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = 'MyDestructableClass';
}
function __destruct() {
print 'Destroying ' . $this->name . "\n";
}
}
$obj = new MyDestructableClass();
?>
Exceptions!
The new OO model will support exceptions ala Java
<?php
class MyException {
function __construct($exception) {
$this->exception = $exception;
}
function Display() {
print "MyException: $this->exception\n";
}
}
class MyExceptionFoo extends MyException {
function __construct($exception) {
$this->exception = $exception;
}
function Display() {
print "MyException: $this->exception\n";
}
}
try {
throw new MyExceptionFoo('Hello');
}
catch (MyException $exception) {
$exception->Display();
}
?>
Object Dereferencing
You can now directly dereference objects
returned from functions.
<?php
class Circle {
function draw() {
print "Circle\n";
}
}
class Square {
function draw() {
print "Square\n";
}
}
function ShapeFactoryMethod($shape) {
switch ($shape) {
case 'Circle': return new Circle();
case 'Square': return new Square();
}
}
ShapeFactoryMethod('Circle')->draw();
ShapeFactoryMethod('Square')->draw();
?>