Objects Are References
<?php
$a = new my_class;
$b = $a;
$c = clone $a;
?>
New constructor/destructor naming
<?php
class my_class {
function __construct() {
echo "Constructor";
}
function __destruct() {
echo "Destructor";
}
}
?>
PPP
<?php
class my_class {
public $a;
protected $b;
private $c;
}
?>
Overloaded Properties and Methods
<?php
class my_class {
function __set($name, $value) { }
function __get($name) { }
function __call($name, $args) { }
}
?>
Object type hinting
<?php
class the_class {
public $var = 'foo';
}
class my_other_class {
public function __construct(the_class $obj) {
echo $obj->var;
}
}
$a = new the_class;
$b = new my_other_class($a);
$c = new my_other_class($b);
?>
foo
Fatal error: Argument 1 must be an instance of the_class in script.php on line 6
Interfaces
<?php
interface abc {
public function setVariable($name, $var);
public function getVariable($name);
}
class my_abc implements abc {
private $vars = array();
public function setVariable($name, $var) {
$this->vars[$name] = $var;
}
public function getVariable($name) {
return $this->vars[$name];
}
}
$test = new my_abc;
$test->setVariable('foo','bar');
echo $test->getVariable('foo');
?>
Output
Bad Interface
<?php
class my_other_class implements abc {
public function setVariable($name, $var) {
$this->vars[$name] = $var;
}
}
?>
Fatal error: Class my_other_class contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (abc::getVariable) script.php on line 2
final
<?php
class my_main_class {
public function foo() { }
final public function bar() { }
}
class my_other_class extends my_main_class {
public function foo() { }
public function bar() { }
}
?>
Fatal error: Cannot override final method my_main_class::bar() in script.php on line 9
Base.inc
<?php
abstract class Base {
public function __toString() {
return '<pre>'.print_r($this,TRUE).'</pre>';
}
}
?>
Autoload
<?php
function __autoload($class) {
echo "autoload called for $class\n";
require "presentations/slides/keynotes/$class.inc";
}
class my_class2 extends Base {
function __construct($arg) {
$this->prop = $arg;
}
}
$test = new my_class2('foo');
echo $test;
?>
Output