PHP 5 Object Oriented Marcus Brger James Cox

  • Slides: 33
Download presentation
PHP 5 Object Oriented Marcus Börger James Cox php|cruise 2004 M. Börger, J. Cox

PHP 5 Object Oriented Marcus Börger James Cox php|cruise 2004 M. Börger, J. Cox PHP 5 Object Oriented

Overview þ PHP 5 vs. PHP 4 þ Is PHP 5 revolutionary? þ PHP

Overview þ PHP 5 vs. PHP 4 þ Is PHP 5 revolutionary? þ PHP 5 OO þ Why is OO a good thing? M. Börger, J. Cox PHP 5 Object Oriented 2

E = mc 2 þ PHP 5 is “faster” than PHP 4 þ Speed

E = mc 2 þ PHP 5 is “faster” than PHP 4 þ Speed by design þ Nitty gritty engine improvements þ Faster callbacks þ Faster comparisons þ Faster Harder Stronger þ New extensions that eliminate userspace code overhead þ PDO þ SQLite þ PHP 4 executes code faster þ New execution architecture slows things down þ Execution architecture isn’t terribly important though M. Börger, J. Cox PHP 5 Object Oriented 3

Revamped OO Model þ PHP 5 has really good OO þ þ þ Better

Revamped OO Model þ PHP 5 has really good OO þ þ þ Better code reuse Better for team development Easier to refactor Some patterns lead to much more efficient code Fits better in marketing scenarios M. Börger, J. Cox PHP 5 Object Oriented 4

PHP 4 and OO ? ¨ Poor Object model þ Methods ý No visibility

PHP 4 and OO ? ¨ Poor Object model þ Methods ý No visibility ý No abstracts, No final ý Static without declaration þ Properties ý No default values ý No static properties þ Inheritance ý No abstract, final inheritance, no interfaces þ Object handling ý Copied by value ý No destructors M. Börger, J. Cox PHP 5 Object Oriented 5

ZE 2's revamped object model þ þ þ Objects are referenced by identifiers Constructors

ZE 2's revamped object model þ þ þ Objects are referenced by identifiers Constructors and Destructors Static members Default property values Constants Visibility Interfaces Final and abstract members Interceptors Exceptions Reflection API Iterators M. Börger, J. Cox PHP 5 Object Oriented 6

Objects referenced by identifiers þ þ Objects are no longer copied by default Objects

Objects referenced by identifiers þ þ Objects are no longer copied by default Objects may be copied using __clone() <? php $obj class Object {}; $obj = new Object(); $ref Instance 1 $dup Instance 2 $ref = $obj; $dup = $obj->__clone(); Class Object ? > M. Börger, J. Cox PHP 5 Object Oriented 7

Constructors and Destructors þ Constructors/Destructors control object lifetime þ Constructors may have both new

Constructors and Destructors þ Constructors/Destructors control object lifetime þ Constructors may have both new OR old style names þ Destructors are called when deleting last reference <? php class Object { function __construct() {} function __destruct() {} } $obj = new Object(); unset($obj); ? > M. Börger, J. Cox PHP 5 Object Oriented 8

Constructors and Destructors þ Parents must be called manually <? php class Base {

Constructors and Destructors þ Parents must be called manually <? php class Base { function __construct() {} function __destruct() {} } class Object extends Base { function __construct() { parent: : __construct(); } function __destruct() { parent: : __destruct(); } } $obj = new Object(); unset($obj); ? > M. Börger, J. Cox PHP 5 Object Oriented 9

Default property values þ Properties can have default values þ Bound to the class

Default property values þ Properties can have default values þ Bound to the class not to the object þ Default values cannot be changed but overwritten <? php class Object { var $prop = "Hellon"; } $obj 1 $obj 2 Instance 1 $prop Instance 2 $prop $obj 1 = new Object; $obj 1 ->prop = "Hello Worldn"; $obj 2 = new Object; echo $obj 2 ->prop; // Hello Class Object $prop/default ? > M. Börger, J. Cox PHP 5 Object Oriented 10

Static members þ Static methods and properties þ Bound to the class not to

Static members þ Static methods and properties þ Bound to the class not to the object þ Can be initialized <? php class Object { var $pop; static $stat = "Hellon"; static function test() { echo self: : $stat; } } $obj 1 $obj 2 Instance 1 $prop Instance 2 $prop Object: : test(); $obj 1 = new Object; $obj 2 = new Object; Class Object $stat ? > M. Börger, J. Cox PHP 5 Object Oriented 11

New pseudo constants þ þ þ __CLASS__ __METHOD__ Self Parent $this shows the current

New pseudo constants þ þ þ __CLASS__ __METHOD__ Self Parent $this shows the current class name shows class and method or function references the class itself references the parent class references the object itself <? php class Base { static function Show() { echo __FILE__. '('. __LINE__. '): '. __METHOD__. "n"; } } class Object extends Base { static function Use() { Self: : Show(); Parent: : Show(); } static function Show() { echo __FILE__. '('. __LINE__. '): '. __METHOD__. "n"; } } ? > M. Börger, J. Cox PHP 5 Object Oriented 12

Visibility þ Controlling member visibility / Information hiding þ A derived class does not

Visibility þ Controlling member visibility / Information hiding þ A derived class does not know inherited privates þ An inherited protected member can be made public <? php class Base { public $a; protected $b; private $c; } class Derived extends Base { public $a; public $b; private $c; } ? > M. Börger, J. Cox PHP 5 Object Oriented Derived Base $a $b $c Base: : $c 13

Constructor visibility þ þ þ A protected constructor prevents instantiation Adding final prevents instantiation

Constructor visibility þ þ þ A protected constructor prevents instantiation Adding final prevents instantiation of child classes Static members may call non public constructors <? php class Base { protected function __construct() { } } class Derived extends Base { // constructor is still protected static function get. Base() { return new Base; // Factory pattern } } class Three extends Derived { // constructor is public, Three may be instantiated public function __construct() { } } ? > M. Börger, J. Cox PHP 5 Object Oriented 14

Clone visibility þ þ A protected __clone prevents external cloning A private final __clone

Clone visibility þ þ A protected __clone prevents external cloning A private final __clone prevents cloning Before __clone is called all properties are copied <? php class Base { <? php protected function __clone() { class Base { } } private final function __clone() { } Derived extends Base { class }public function __clone() { class Derived Base { return extends new Base; } // public function __clone() { // return function new Base; copy. Base() { public static // } return Base: : __clone(); } // public static function copy. Base() { return Base: : __clone(); } // ? > // } } ? > M. Börger, J. Cox PHP 5 Object Oriented 15

Constants þ þ Constants are read only static members Constants are always public <?

Constants þ þ Constants are read only static members Constants are always public <? php class Base { const greeting = "Hellon"; } class Dervied extends Base { const greeting = "Hello Worldn"; static function func() { echo parent: : greeting; } } echo Base: : greeting; echo Derived: : greeting; Derived: : func(); ? > M. Börger, J. Cox PHP 5 Object Oriented 16

Abstract members þ þ Properties cannot be made abstract Methods can be abstract þ

Abstract members þ þ Properties cannot be made abstract Methods can be abstract þ They cannot have a body (aka default implementation) þ A class with an abstract method must be abstract þ Classes can be made abstract þ Those classes cannot be instantiated <? php abstract class Base { abstract function no_body(); } class Derived extends Base { function no_body() { echo "Bodyn"; } } ? > M. Börger, J. Cox PHP 5 Object Oriented 17

Final members þ Methods can be made final þ They cannot be overwritten þ

Final members þ Methods can be made final þ They cannot be overwritten þ They are class invariants þ Classes can be made final þ They cannot be inherited <? php class Base { final function invariant() { echo "Hellon"; } } class Derived extends Base { } final class Leaf extends Derived { } ? > M. Börger, J. Cox PHP 5 Object Oriented 18

Interfaces þ þ Interfaces describe an abstract class protocol Classes may inherit multiple Interfaces

Interfaces þ þ Interfaces describe an abstract class protocol Classes may inherit multiple Interfaces <? php Container Iterator interface Drawable { function draw(); } class Line implements Drawable { Drawable function draw() {}; } interface Container { function insert($elem); } class Circle implements Drawable { Polygon Line function draw() {}; } class Ellipse extends Circle { function draw() {}; } class Polygon implements Drawable, Container, Iterator { function draw() { /* foreach */ }; } ? > M. Börger, J. Cox PHP 5 Object Oriented Circle Ellipse 19

Property types þ Declared properties þ May have a default value þ Can have

Property types þ Declared properties þ May have a default value þ Can have selected visibility þ Implicit public properties þ Declared by simply using them in ANY method þ Virtual properties þ Handled by interceptor methods þ Static properties M. Börger, J. Cox PHP 5 Object Oriented 20

Object to String conversion þ __to. String(): automatic object string conversion <? php class

Object to String conversion þ __to. String(): automatic object string conversion <? php class Object { function __to. String() { return 'Object as string'; } } $o = new Object; echo $o; $str = (string) $o; ? > M. Börger, J. Cox PHP 5 Object Oriented 21

Interceptors þ Allow to dynamically handle non class members þ Lazy initialization of properties

Interceptors þ Allow to dynamically handle non class members þ Lazy initialization of properties þ Simulating Object aggregation, Multiple inheritance <? php class Object { protected $virtual = array(); function __get($name) { return @$virtual[$name]; } function __set($name, $value) { $virtual[$name] = $value; } function __call() { echo 'Could not call '. __CLASS__. ': : '. $func. "n"; } } ? > M. Börger, J. Cox PHP 5 Object Oriented 22

Exceptions þ Respect these rules 1. Exceptions are exceptions 2. Never use exceptions for

Exceptions þ Respect these rules 1. Exceptions are exceptions 2. Never use exceptions for control flow 3. Never use exceptions for parameter passing <? php try { // your code throw new Exception(); } catch (Exception $e) { // exception handling } ? > M. Börger, J. Cox PHP 5 Object Oriented 23

Exception specialization þ þ Exception must be derived from class exception Exceptions should be

Exception specialization þ þ Exception must be derived from class exception Exceptions should be specialized <? php class Your. Exception extends Exception { } try { // your code throw new Your. Exception(); } catch (Your. Exception $e) { // exception handling } catch (Exception $e) { // exception handling } ? > M. Börger, J. Cox PHP 5 Object Oriented 24

Exception specialization þ þ Exception blocks can be nested Exceptions can be rethrown <?

Exception specialization þ þ Exception blocks can be nested Exceptions can be rethrown <? php class Your. Exception extends Exception {}; try { // your code throw new Your. Exception(); } catch (Your. Exception $e) { // exception handling throw $e; } catch (Exception $e) { // exception handling } } catch (Your. Exception $e) { // exception handling } ? > M. Börger, J. Cox PHP 5 Object Oriented 25

Constructor failure þ Constructors do not return the created object Æ Overriding $this as

Constructor failure þ Constructors do not return the created object Æ Overriding $this as in PHP 4 is no longer possible þ Exceptions allow to handle failed constructors <? php class Object { function __construct() { throw new Exception; } } try { $o = new Object; } catch (exception $e) { echo "Object could not be instantiatedn"; } ? > M. Börger, J. Cox PHP 5 Object Oriented 26

Reflection API þ Can reflect nearly all aspects of your PHP code þ Functions

Reflection API þ Can reflect nearly all aspects of your PHP code þ Functions þ Classes, Methods, Properties þ Extensions <? php class Foo { public $prop; function Func($name) { echo "Hello $name"; } } Reflection. Class: : export('Foo'); Reflection. Object: : export(new Foo); Reflection. Method: : export('Foo', 'func'); Reflection. Property: : export('Foo', 'prop'); Reflection. Extension: : export('standard'); ? > M. Börger, J. Cox PHP 5 Object Oriented 27

Why else þ Simplify situations where a lot of stuff may fail <? php

Why else þ Simplify situations where a lot of stuff may fail <? php if (@$db=sqlite_open($dbname)) { if (@$res = sqlite_query()) { // handle result } } } if (sqlite_last_error($db)) { // error handling } ? > M. Börger, J. Cox try { $db = new sqlite_db($dbname); $res = sqlite_query(); // handle result $res = sqlite_query(): // handle result } catch (sqlite_exception $err) { // error handling } ? > PHP 5 Object Oriented 28

Iterators þ þ Some objects can be iterated Others show their properties <? php

Iterators þ þ Some objects can be iterated Others show their properties <? php class Object { public $prop 1 = "Hello"; public $prop 2 = "Worldn"; } foreach(new Object as $prop) { echo $prop; } ? > M. Börger, J. Cox PHP 5 Object Oriented 29

Typehinting þ PHP 5 allows to easily force a type of a parameter þ

Typehinting þ PHP 5 allows to easily force a type of a parameter þ NULL is allowed with typehints <? php class Object { public function compare(Object $other) { // Some code here } } ? > M. Börger, J. Cox PHP 5 Object Oriented 30

Iterators þ þ Engine internal Iterator <? php User Iterators <? php interface Iterator

Iterators þ þ Engine internal Iterator <? php User Iterators <? php interface Iterator { function rewind(); function valid(); function current(); function key(); function next(); } ? > class Filter { Iterator{ abstract $it =My. Filter get_resource(); class extends Filter implements function __construct(Iterator foreach($it as $key=>$val) { $it, $rex){ $input). . . Filter: : __construct($it); function // accees rewind(). . . data $this->rex = $rex; } abstract function accept(); } ? > function valid(). . . function accept() { current(). . . returnkey(). . . preg_match($this->rex, function $this->current()); function next(). . . }} } ? >? > <? php $it = get_resource(); foreach(new for ($it->rewind(); My. Filter($it, $it->valid(); $regular_expression) $it->next()) { as $key=>$val) { // access $value = $it->current(); filtered data only $key = $it->key(); } ? > M. Börger, J. Cox PHP 5 Object Oriented 31

New extensions þ New OO extensions and state/schedule þ þ þ FFI PECL /

New extensions þ New OO extensions and state/schedule þ þ þ FFI PECL / 5. 0 Date PECL / 5. 1? DOM built-in, default / 5. 0 My. SQLi built-in / 5. 0 PDO 5. 1? PIMP 5. 0? Simple. XML built-in, default / 5. 0 SOAP built-in / 5. 0 SPLbuilt-in, default / 5. 0 SQLite built-in, default / 5. 0 Tidy built-in, default / 5. 0 XSL built-in / 5. 0 M. Börger, J. Cox PHP 5 Object Oriented 32

Resources þ http: //php. net þ http: //zend. com M. Börger, J. Cox PHP

Resources þ http: //php. net þ http: //zend. com M. Börger, J. Cox PHP 5 Object Oriented 33