PHP 5 Marcus Brger Sterling Hughes international PHP
PHP 5 Marcus Börger Sterling Hughes international PHP 2003 conference Marcus Börger PHP 5
Overview þ PHP 5 vs PHP 4 þ Is PHP 5 revolutionary? þ PHP 5 OO þ Why is OO a good thing? þ PHP 5 and Databases þ PHP 5 and XML Marcus Börger PHP 5 2
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 Marcus Börger PHP 5 3
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 Marcus Börger PHP 5 4
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 Marcus Börger PHP 5 5
The Solution to all your problems þ PHP 4’s XML was pathetic þ SAX was OK þ DOM was crappy, DOM was fake þ There was nothing else þ PHP 5 XML is brilliant þ þ Bow SAX is OK DOM is functional Simple. XML is the solution to all your problems Marcus Börger PHP 5 6
Other Stuff þ PHP 5 has much improved streams support þ Stream filters þ Engine level integration þ Stream “Servers” þ þ þ PHP 5 PHP 5 will have a packaging system has completely new XML support has a new database abstraction api supports embedded databases has an improved CLI has a new imaging system (PIMP) Marcus Börger PHP 5 7
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 Marcus Börger PHP 5 8
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; Class Object $dup = $obj->__clone(); ? > Marcus Börger PHP 5 9
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); ? > Marcus Börger PHP 5 10
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); ? > Marcus Börger PHP 5 11
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"; Class Object $prop/default $obj 2 = new Object; echo $obj 2 ->prop; // Hello ? > Marcus Börger PHP 5 12
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; } } Object: : test(); $obj 1 = new Object; $obj 2 = new Object; ? > Marcus Börger PHP 5 $obj 1 $obj 2 Instance 1 $prop Instance 2 $prop Class Object $stat 13
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"; } } ? > Marcus Börger PHP 5 14
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; } ? > Marcus Börger PHP 5 Derived Base $a $b $c Base: : $c 15
Constructor visibility þ A protected constructor prevents instantiation <? 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 { public function __construct() { } } ? > Marcus Börger PHP 5 16
Clone visibility þ þ A protected __clone prevents external cloning A private final __clone prevents cloning <? php class Base { protected private final function __clone($that) { { } } } class Derived extends Base { public // public function __clone($that) { { // return new Base; } // } public // public static function copy. Base($that) { { // return Base: : __clone($that); } // } } ? > Marcus Börger PHP 5 17
Constants þ þ Constants are read only static properties 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(); ? > Marcus Börger PHP 5 18
Abstract members þ þ Properties cannot be made abstract Methods can be abstract þ They don’t have a body þ A class with an abstract method must be abstract þ Classes can be made abstract þ The class cannot be instantiated <? php abstract class Base { abstract function no_body(); } class Derived extends Base { function no_body() { echo "Bodyn"; } } ? > Marcus Börger PHP 5 19
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 { } ? > Marcus Börger PHP 5 20
Interfaces þ þ Interfaces describe an abstract class protocol Classes may inherit multiple Interfaces <? php interface Drawable { function draw(); } class Line implements Drawable { function draw() {}; } class Rect implements Drawable { function draw() {}; } class Circle implements Drawable { function draw() {}; } class Ellipse extends Circle { function draw() {}; } ? > Marcus Börger PHP 5 Drawable Line Rect Circle Rect 21
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 Marcus Börger PHP 5 22
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; ? > Marcus Börger PHP 5 23
Interceptors þ Allow to dynamically handle non class members þ Lazy initialization of properties þ Simulating Object aggregation, Multiple inheritance <? php class Object { protected $virtual; function __get($name) { return @$virtual[$name]; } function __set($name, $value) { $virtual[$name] = $value; } function __call() { echo 'Could not call '. __CLASS__. ': : '. $func. "n"; } } ? > Marcus Börger PHP 5 24
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 } ? > Marcus Börger PHP 5 25
Exception specialization þ 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 } ? > Marcus Börger PHP 5 26
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 } ? > Marcus Börger PHP 5 27
Constructor failure þ þ Constructors do not return the created object 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"; } ? > Marcus Börger PHP 5 28
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'); ? > Marcus Börger PHP 5 29
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; } ? > Marcus Börger PHP 5 30
Iterators þ þ Internal Iterators User Iterators <? php interface Iterator { function rewind(); function has. More(); function current(); function key(); function next(); } ? > <? php class Filter implements Iterator { function __construct(Iterator $input). . . function rewind(). . . function accept($value). . . function has. More(). . . <? php function current(). . . $it = get_resource(); function key(). . . foreach($it as $key=>$val) { function // accessnext(). . . data } ? > <? php $it = get_resource(); foreach(new for ($it->rewind(); Filter($it, $it->has. More(); $filter_param)$it->next()) as $key=>$val) { { // access $value = $it->current(); filtered data only $key = $it->key(); } ? > Marcus Börger PHP 5 31
PHP & UML Marcus Börger PHP 5 32
PHP & UML Marcus Börger PHP 5 33
PHP & UML Marcus Börger PHP 5 34
Typehinting þ PHP 5 allows to easily force a type of a parameter þ Beta 1 and beta 2 allow NULL with typehints þ Beta 3 will have a syntax to decide about NULL <? php class Object { public function compare(Object $other) { // Some code here } } ? > Marcus Börger PHP 5 35
CLI in PHP 4 ¨ Direct code execution php [options] –r code [[--] args. . . ] ¨ Interactive mode php -a Marcus Börger PHP 5 36
Improved CLI in PHP 5 ¨ Line by line input processing php [-B code] –R code [-E code] [[--] args] ¨ Line by line input processing with scripts php [-B code] –F code [-E code] [[--] args] Marcus Börger PHP 5 37
Counting source lines Try find –regex '. *. [ch]' –exec wc -l {} ; for i in ‘find -regex '. *. [ch]' ‘; do wc –l $i; done; Do Do find –regex '. *. [ch]' | xargs wc –l find –regex '. *. [ch]' -exec wc –l {} ; | awk '{L=L+$1} END { print L }' find –regex '. *. [ch]' | php –R '@$L+=count(file($argn)); ' -E 'echo "$Ln"; ' Do Marcus Börger PHP 5 38
CLI meets CVS ¨ Search for locally modified files cvs –n up 2>/dev/null | awk '/M / {print $2}' cvs –n up 2>/dev/null | php –R 'ereg("^M ", $argn) && print(substr($argn, 2)). "n"; ' Marcus Börger PHP 5 39
CLI meets CVS ¨ CVS clean cvs up -C cvs –n up 2>/dev/null | php –R 'ereg("^[MA] ", $argn) && system("rm –f ". substr($argn, 2)); ' Marcus Börger PHP 5 40
CLI and PHP 5 ? ¨ New oo features provide new solutions php –r 'foreach(new Directory. Iterator($argv[1]) as $f) echo "$fn"; ' php –r 'include "dba. inc"; $db=new dba($argv[1]); $db[$argv[2]=$argv[3]; ' Marcus Börger PHP 5 41
GTK: php. Mole Marcus Börger PHP 5 42
GTK: Agata. Report Marcus Börger PHP 5 43
GTK: Newz. Rider Marcus Börger PHP 5 44
Resources þ man php þ http: //www. php. net/features. commandline þ http: //gtk. php. net Marcus Börger PHP 5 45
New extensions þ þ þ þ FFI Date DOM My. SQLi PDO PHILI Simple. XML SOAP SPL SQLite Tidy XML + XSL Marcus Börger PHP 5 46
FFI þ Native Function Call Interface þ Written by Wez Furlong… <? php $lib = new FFI_Library('libc 6. so'); echo $lib->strlen("Hello World"); ? > Marcus Börger PHP 5 47
New extensions: DOM, Simple. XML, XSL þ PHP 5 will use lib. XML 2 instead of expat þ ext/DOM fully confirms to W 3 C standards and replaces ext/DOMXML þ ext/XSL is based on ext/DOM and replaces ext/XSLT þ ext/Simple. XML is the simple PHP way to access XML Data Marcus Börger PHP 5 48
New extensions: My. SQLi þ Mysql grows to become more and more an enterprise ready DBMS but sticks to its origin fastness, easiness þ PHP 5 reflects this development by providing a new extension named My. SQLi þ Support for My. SQL embedded into PHP ? Profiling Marcus Börger PHP 5 49
New extensions: SQLite þ Started in 2000 by D. Richard Hipp Single file database Subselects, Triggers, Transactions, Views Very fast, 2 -3 times faster than My. SQL, Postgre. SQL for many common operations 2 TB data storage limit ý ý ý Views are read-only No foreign keys Locks whole file for writing þ þ Marcus Börger PHP 5 50
New extensions: SQLite þ þ þ þ PHP extension bundled with PHP 5 Available via PECL since PHP 4. 3 Used on php. net SQLite library integrated with PHP extension API designed to be logical, easy to use High performance Convenient migration from other PHP database extensions Call PHP code from within SQL Marcus Börger PHP 5 51
New extensions: SPL þ SPL aka Standard PHP Library þ þ þ Iterators Filters Standard internal classes Marcus Börger PHP 5 52
New extensions: PDO þ þ PDO aka PHP Data Objects Provides an object oriented unified way of accessing data from different sources þ þ þ Limit support/emulation through ext/spl Profiling Precompiled statements Marcus Börger PHP 5 53
New extensions: PIMP þ þ þ þ A better version of GD Fast: 2 X. . . 100 X Less memory usage and allocation calls Object oriented "old fashion" bitmap features (~90% compatible) Fast image filters Own plug-in mechanism þ Libcairo þ XWindow þ PDF 1. 4 þ Postscript Marcus Börger PHP 5 54
- Slides: 54