SlideShare a Scribd company logo
Stephan Schmidt, 1&1 Internet AG Go OO! Real-Life Design Patterns in PHP5
Agenda What are Design Patterns? PHP5's new OO features Creational Patterns Structural Patterns Behavioral Patterns A Look at SPL Real-Life Appliances
The speaker Working for 1&1 Internet AG Founding member of PHP Application Tools (pat) Active developer of the PEAR community Writing for several PHP magazines
What are Design Patterns? standard solutions to common problems in software architecture independent of the programming language ease the communication between the developers typically describe the interaction of classes and objects
PHP5's new OO features OO-model has been completely revamped objects are not any longer just "improved arrays" new SPL extension
Pass-by-reference class Foo { var $val = 'Foo'; } $foo = new Foo(); $bar = $foo; $bar->val = 'Bar'; echo $bar->val . "\n"; echo $foo->val . "\n"; Bar Foo Bar Bar PHP 5 PHP 4
Constructors/Destructors Constructor is named __construct() Destructor is named __destruct() class Foo { public function __construct() { print "Foo created\n"; } public function __destruct() { print "Foo destroyed\n"; } } $bar = new Foo(); unset($bar);
Visibility Restrict access to properties and methods of a class. public  := all protected := class and descendants private  := only the class
Visibility (2): Example class Foo { private  $foo = 'foo'; public  $bar = 'bar'; protected $tomato = 'tomato'; } $bar = new Foo(); print "{$bar->bar}\n"; print "{$bar->foo}\n"; $ php visibility.php  bar Fatal error: Cannot access private property Foo::$foo in /home/schst/go-oo/visibility.php on line 9
Static methods/properties Available outside of the object context can be public/private/protected self::$foo instead of $this->foo class Foo { public static $foo = 'bar'; public static function getFoo() { return self::$foo; } } print Foo::$foo . "\n"; print Foo::getFoo() . "\n";
Object Abstraction Abstract classes cannot be instantiated abstract class AbstractClass { abstract public function doSomething(); } class ConcreteClass extends AbstractClass { public function doSomething() { print "I've done something.\n"; } } $foo = new ConcreteClass(); $bar = new AbstractClass(); Fatal error: Cannot instantiate abstract class AbstractClass in ... on line 11
Interfaces Specify the methods a class has to implement without defining how they are implemented Classes may implement more than one interface If a class does not implement all methods of an interface => E_FATAL
Interfaces (2): Example interface IRequest { public function getValue($name); public function getHeader($name); } class HttpRequest implements IRequest { public function getValue($name) { return $_REQUEST[$name]; } } Fatal error: Class HttpRequest contains 1 abstract methods and must therefore be declared abstract (IRequest::getHeader) in /home/schst/go-oo/interfaces.php on line 10
Property Overloading Intercept access to properties, that do not exist class Foo { private $props = array('foo' => 'bar'); public function __get($prop) { if (!isset($this->props[$prop])) { return null; } return $this->props[$prop]; } } $foo = new Foo(); print "{$foo->foo}\n";
Method Overloading Intercept calls to methods, that do not exist class Foo { public function __call($method, $args) { if (is_callable($method)) { return call_user_func_array($method, $args); } return null; } } $foo = new Foo(); print $foo->strrev('tomato') . "\n";
__toString() Change string-cast behavior of an object class User { private $id; private $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function __toString() { return "{$this->name} ({$this->id})\n"; } } $schst = new User('schst', 'Stephan Schmidt'); print $schst;
Object Iteration Objects can be used in foreach-statements class PearDevelopers { public $schst  = 'Stephan Schmidt'; public $luckec = 'Carsten Lucke'; } $users = new PearDevelopers(); foreach ($users as $id => $name) { print "$id is $name\n"; } schst is Stephan Schmidt luckec is Carsten Lucke
Misc additions Object cloning $bar = clone $foo; Type hints for objects public function foo (MyClass $obj) {} Exception Handling Reflection API __autoload() SPL
Creational Patterns Create objects Hide the creational process Your application does not need to know, how the object has to be created Allows you to hide the concrete implementation Dynamic object configuration
Factory Method Create objects based on some input Hides the object creation PEAR makes heavy use of this, like in PEAR::DB $con = DB::connect('mysql://user:pass@host/db'); The returned object is an instance of a subclass of DB_common based on the database type
Singleton Makes sure that only one instance of a class exists class Registry { private static $instance = null; private function __construct() {} public $foo; public function singleton() { if (is_null(self::$instance)) { self::$instance = new Registry(); } return self::$instance; } }
Singleton (2): Usage Commonly used for Configurations / Registries Sharing DB connections (combine it with factory method) Request objects … $reg1 = Registry::singleton(); $reg2 = Registry::singleton(); $reg1->foo = 'Bar'; print $reg2->foo . "\n";
Structural Patterns Define the relationships between classes and/or objects Object-composition Often use inheritance and interfaces
Decorator Allows you to dynamically add functionality to an object Used for functionality that is used only in some cases Often used to avoid inheritance or when inheritance is not possible Decorator wraps the original objects
Decorator (2): Component class String { private $string = null; public function __construct($string)  { $this->string = $string; } public function __toString()  { return $this->string; } public function getLength() { return strlen($this->string); } public function getString() { return $this->string; } public function setString($string) { $this->string = $string; } }
Decorator (3): Abstract An abstract decorator abstract class String_Decorator { protected $obj; public function __construct($obj) { $this->obj = $obj; } public function __call($method, $args) { if (!method_exists($this->obj, $method)) { throw new Exception('Unknown method called.'); } return call_user_func_array( array($this->obj, $method), $args); } }
Decorator (4): Bold Concrete Decorator class String_Decorator_Bold extends String_Decorator { public function __toString() { return '<b>' . $this->obj->__toString() . '</b>'; } } Usage $str  = new String('Decorators are cool'); $strBold = new String_Decorator_Bold($str); print $strBold;
Decorator (5): Reverse Adding a method class String_Decorator_Reverse extends String_Decorator { public function reverse(){ $str = $this->obj->getString(); $this->obj->setString(strrev($str)); } } Usage $str  = new String('Decorators are cool'); $strRev = new String_Decorator_Reverse($str); $strRev->reverse(); print $strRev;
Decorator (6): Combination Combining decorators $str  = new String('Decorators are cool'); $strBold = new String_Decorator_Bold($str); $strRev  = new String_Decorator_Reverse($strBold); $strRev->reverse(); print $strRev;
Proxy Provides a placeholder for an objects to control or access this objects Very common with webservices or application servers $client = new SoapClient( 'http://api.google.com/GoogleSearch.wsdl'); $result = $client->doGoogleSearch(…);
Proxy (2): Implementation Catch all method calls on the Proxy using __call() class Proxy { public function __construct() { // establish connection to the original object } public function __call($method, $args) { // forward the call to the original object // using any protocol you need } }
Delegator Similar to Proxy Often referred to as &quot;Chained Proxy&quot; Acts as a proxy to several objects Intercept calls to all unknown methods and forward those to any of the delegates, that provides these methods Ready-to-use implementation for PHP: PEAR_Delegator
Delegator (2): Example The Delegator class Foo extends PEAR_Delegator { public function __construct() { parent::_construct(); } public function __destruct() { parent::__destruct(); } public function displayFoo() { print &quot;foo\n&quot;; } }
Delegator (3): Example The Delegates class Delegate1 { public function displayBar() { print &quot;bar\n&quot;; } } class Delegate2 { public function displayTomato() { print &quot;tomato\n&quot;; } }
Delegator (4): Example Usage $delegator = new Foo(); $delegate1 = new Delegate1(); $delegate2 = new Delegate2(); $delegator->addDelegate($delegate1); $delegator->addDelegate($delegate2); $delegator->displayFoo(); $delegator->displayBar(); $delegator->displayTomato();
Behavioral patterns Provide solutions for communication between objects Increase the flexibility of the communication
Observer Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.  First step to event-based development Often used for logging techniques
Observer (2): Subject class Subject { private $observers = array(); public $state = null; public function attach(Observer $observer) { $this->observers[] = $observer; } public function detach(Observer $observer) { // remove the observer } public function notify() { for ($i = 0; $i < count($this->observers); $i++) { $this->observers[$i]->update(); } } }
Observer (3): Observer class Observer { private $subject; private $name public function __construct($subject, $name) { $this->subject = $subject; $this->name  = $name; } public function update() { $state = $this->subject->state; print $this->name.&quot;: State of subject is $state\n&quot;; } }
Observer (4): Usage $subj = new Subject(); $ob1  = new Observer($subj, 'Observer 1'); $ob2  = new Observer($subj, 'Observer 2'); $subj->attach($ob1); $subj->attach($ob2); $subj->state = &quot;authenticated&quot;; $subj->notify();
Standard PHP Library Bundled with PHP 5 and enabled by default Collection of interfaces Iterators ArrayAccess, Countable Subject/Observer (PHP 5.1) Collection of Classes Iterators Exceptions, FileObject (PHP 5.1)
ArrayAccess Allows you to access any object as it were an array Interface provided by SPL public function offsetExists($offset); public function offsetGet($offset); public function offsetSet($offset, $value); public function offsetUnset($offset); Only works with [] array syntax
ArrayAccess (2): Example class Foo implements ArrayAccess { private $props = array('foo' => 'Bar'); public function offsetExists($offset) { return isset($this->props[$offset]); } public function offsetGet($offset) { return $this->props[$offset]; } public function offsetSet($offset, $value) { $this->props[$offset] = $value; } public function offsetUnset($offset) { unset($this->props[$offset]); } }
ArrayAccess (3): Example $obj = new Foo(); print $obj['foo'] . &quot;\n&quot;; $obj['bar'] = 3452; if (isset($obj['bar'])) { print $obj['bar'] . &quot;\n&quot;; } $ php arrayAccess.php Bar 3452
Abstracting HTTP Requests Create request object to access request properties Replaces $_GET, $_POST, $_SERVER High level of abstraction Provides flexibility Implements ArrayAccess interface to allow $request['property'] syntax for a shallow learning curve
Request (2): Example abstract class Request implements ArrayAccess { protected $properties = array(); public function offsetExists($offset) { return isset($this->properties[$offset]); } public function offsetGet($offset) { return $this->properties[$offset]; } public function offsetSet($offset, $value) { $this->properties[$offset] = $value; } public function offsetUnset($offset) { unset($this->properties[$offset]); }  }
Request (3): HTTP class Request_HTTP extends Request  { public function __construct() { $this->properties = $_REQUEST; } } $request = new Request_HTTP(); if (isset($request['foo'])) { echo $request['foo'];  } else { echo &quot;property foo has not been set&quot;; } http://www.example.com/?foo=bar
Replacing the Request Request_HTTP can be replaced by any class with the same interface Request_CLI Request_SOAP Combine this with singleton and factory method: $request = Request::get('HTTP');
Request (4): CLI class Request_CLI extends Request  { public function __construct() { array_shift($_SERVER['argv']); foreach ($_SERVER['argv'] as $pair) { list($key, $value) = explode('=', $pair); $this->properties[$key] = $value; } } } $request = new Request_CLI(); if (isset($request['foo'])) { echo $request['foo'];  } else { echo &quot;property foo has not been set&quot;; } $ ./script.php foo=bar
Intercepting filters Allows you to preprocess the request data Apply centralized authentication mechanism Validate/modify request properties Forward based on the URI Borrowed from J2EE Servlets
Intercepting filters (2) abstract class Request implements ArrayAccess { … protected $filters = array(); public function addFilter(InterceptingFilter $filter) { $this->filters[] = $filter; } protected function applyFilters() { for ($i = 0; $i < $this->filters; $i++) { $this->filters[$i]->doFilter($this); } } } Changes to Request
Intercepting filters (3) Simple filter interface class Request_HTTP extends Request { public function __construct() { $this->properties = $_REQUEST; $this->applyFilters(); } } interface InterceptingFilter { public function doFilter(Request $request); } Changes to Request_HTTP
Iterators Interface that allows you to influence the way foreach-iterates over an object mixed public function current(); mixed public function key(); void public function next(); boolean public function valid(); void public function rewind(); Traverse any data using foreach (dir listing, array, text file, etc.)
Iterators (2): Example class CSVFile implements Iterator { protected $file; protected $fp; protected $line; protected $key = -1; public function __construct($file) { $this->file  = $file; $this->fp  = @fopen($this->file, 'r'); if (!$this->fp) { throw new Exception('Could not open file.'); } } public function __desctruct() { fclose($this->fp); }
Iterators (3): Example cont. public function next(){ if (!feof($this->fp)) { $this->key++; $this->line = fgetcsv($this->fp); $this->valid = true; } else { $this->valid = false; } } public function rewind() { $this->key = -1; fseek($this->fp, 0); $this->next(); } }
Iterators (4): Example cont. public function current() { return $this->line; } public function key() { return $this->key; } public function valid() { return $this->valid; } }
Iterators (5): Example cont. $csvFile = new CSVFile('users.csv'); foreach ($csvFile as $entry) { print_r($entry); } Array ( [0] => Array ( [0] => 'schst', [1] => 'Stephan Schmidt' ), [1] => Array ( [0] => 'luckec', [1] => 'Carsten Lucke' ), )
Recursive Iterators Extends the Iterator interface boolean public function hasChildren(); Iterator public function getChildren(); getChildren() returns an object that implements Iterator Traverse hierarchical data using the class RecursiveIteratorIterator
Abstracting data structures Iterators allow you to abstract hierarchical structures in your application using foreach. The user of the data does not know how it is computed, but only traverses it. Simple example: Page definition files in a MVC-framework.
Example: Page defintions One page per configuration file: title = &quot;Homepage&quot; desc  = &quot;This is the homepage&quot; class = &quot;Homepage&quot; Navigation structure in the filesystem: index.ini projects.ini projects/ pat.ini pear.ini pear/ services_ebay.ini xml_serializer.ini
Example: Page Class class Page { public $name; public $title; public $desc; public function __construct($basePath, $name) { $fname = $basePath . '/' . $name . '.ini'; $tmp  = parse_ini_file($fname); $this->name  = $name; $this->title = $tmp['title']; $this->desc  = $tmp['desc']; } } $home = new Page('pages', 'index'); print $home->title;
Example: Sitemap Class class Sitemap implements Iterator { protected $path; protected $pos = 0; protected $pages = array(); public function __construct($path) { $this->path = $path; if (file_exists($this->path)) { $dir = dir($path); while ($entry = $dir->read()) { $this->pages[] = new Page($this->path, $entry); } } } …
Example: Sitemap Class (2) public function current() { return $this->pages[$this->pos]; } public function key() { return $this->pos; } public function next() { ++$this->pos; } public function rewind() { $this->pos = 0; } public function valid() { return isset($this->pages[$this->pos]); } }
Example: Sitemap Usage User does not need to know when and how the page objects are created and in which order they are traversed. INI files can be substituted with XML, content from a database, etc. $sitemap = new Sitemap('pages'); foreach ($sitemap as $page) { echo $page->title . &quot;<br />\n&quot;; }
Example: Going recursive class Page extends Sitemap { …  public function __construct($basePath, $name) { $fname = $basePath . '/' . $name . '.ini'; $tmp  = parse_ini_file($fname); $this->name  = $name; $this->title = $tmp['title']; $this->desc  = $tmp['desc']; $subPath = $basePath . '/' . $this->name; parent::__construct($subPath);  } public function hasPages() { return !empty($this->pages); } }
Example: Going recursive Restricted to two levels :( Not intuitive  $sitemap = new Sitemap('pages'); foreach ($sitemap as $page) { echo $page->title . '<br />'; foreach ($page as $subPage) { echo ' - ' . $subPage->title . '<br />'; } }
Example: Going recursive class Sitemap implements  RecursiveIterator  { … public function hasChildren() { return $this->pages[$this->pos]->hasPages(); } public function getChildren() { return $this->pages[$this->pos]; } }
Example: Done $sitemap  = new Sitemap('pages'); $iterator = new RecursiveIteratorIterator($sitemap,  RIT_SELF_FIRST); foreach ($iterator as $page) { $depth = $iterator->getDepth(); if ($depth > 0) { echo str_repeat('&nbsp;', $depth*2) . ' - '; } echo $page->title . '<br />'; } Homepage Projects - PAT-Projects - PEAR-Projects - Services_Ebay - XML_Serializer
Useful Resources Design Patterns http://www.dofactory.com/Patterns/Patterns.aspx phpPatterns() http://www.phppatterns.com J2EE Patterns http://java.sun.com/blueprints/corej2eepatterns/ Microsoft patterns http://msdn.microsoft.com/architecture/patterns/
The end Thank you for your attention!  ANY QUESTIONS ? [email_address] http://www.php-tools.net Stephan Schmidt, 1&1 Internet AG

More Related Content

What's hot (20)

PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
Shashank Skills Academy
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
kalaisai
 
Php mysql
Php mysqlPhp mysql
Php mysql
Abu Bakar
 
XML and PHP 5
XML and PHP 5XML and PHP 5
XML and PHP 5
Adam Trachtenberg
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
Pamela Fox
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3
Bradley Holt
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
Jussi Pohjolainen
 
Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
Deblina Chowdhury
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
Aminiel Michael
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
Yoeung Vibol
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
mussawir20
 
Introduction to php web programming - get and post
Introduction to php  web programming - get and postIntroduction to php  web programming - get and post
Introduction to php web programming - get and post
baabtra.com - No. 1 supplier of quality freshers
 
lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
tutorialsruby
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
Santhiya Grace
 

Viewers also liked (13)

Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your code
Elizabeth Smith
 
Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)
Mark Baker
 
Refactoring
RefactoringRefactoring
Refactoring
Artem Tabalin
 
Design patterns in Magento
Design patterns in MagentoDesign patterns in Magento
Design patterns in Magento
Divante
 
Six Principles of Software Design to Empower Scientists
Six Principles of Software Design to Empower ScientistsSix Principles of Software Design to Empower Scientists
Six Principles of Software Design to Empower Scientists
David De Roure
 
Software design principles for evolving architectures
Software design principles for evolving architecturesSoftware design principles for evolving architectures
Software design principles for evolving architectures
Firat Atagun
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
Fabien Potencier
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Andreas Enbohm
 
Base SAS Exam Questions
Base SAS Exam QuestionsBase SAS Exam Questions
Base SAS Exam Questions
guestc45097
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
Sergey Karpushin
 
Surprising failure factors when implementing eCommerce and Omnichannel eBusiness
Surprising failure factors when implementing eCommerce and Omnichannel eBusinessSurprising failure factors when implementing eCommerce and Omnichannel eBusiness
Surprising failure factors when implementing eCommerce and Omnichannel eBusiness
Divante
 
Magento scalability from the trenches (Meet Magento Sweden 2016)
Magento scalability from the trenches (Meet Magento Sweden 2016)Magento scalability from the trenches (Meet Magento Sweden 2016)
Magento scalability from the trenches (Meet Magento Sweden 2016)
Divante
 
Omnichannel Customer Experience
Omnichannel Customer ExperienceOmnichannel Customer Experience
Omnichannel Customer Experience
Divante
 
Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your code
Elizabeth Smith
 
Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)Php data structures – beyond spl (online version)
Php data structures – beyond spl (online version)
Mark Baker
 
Design patterns in Magento
Design patterns in MagentoDesign patterns in Magento
Design patterns in Magento
Divante
 
Six Principles of Software Design to Empower Scientists
Six Principles of Software Design to Empower ScientistsSix Principles of Software Design to Empower Scientists
Six Principles of Software Design to Empower Scientists
David De Roure
 
Software design principles for evolving architectures
Software design principles for evolving architecturesSoftware design principles for evolving architectures
Software design principles for evolving architectures
Firat Atagun
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
Fabien Potencier
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Andreas Enbohm
 
Base SAS Exam Questions
Base SAS Exam QuestionsBase SAS Exam Questions
Base SAS Exam Questions
guestc45097
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
Sergey Karpushin
 
Surprising failure factors when implementing eCommerce and Omnichannel eBusiness
Surprising failure factors when implementing eCommerce and Omnichannel eBusinessSurprising failure factors when implementing eCommerce and Omnichannel eBusiness
Surprising failure factors when implementing eCommerce and Omnichannel eBusiness
Divante
 
Magento scalability from the trenches (Meet Magento Sweden 2016)
Magento scalability from the trenches (Meet Magento Sweden 2016)Magento scalability from the trenches (Meet Magento Sweden 2016)
Magento scalability from the trenches (Meet Magento Sweden 2016)
Divante
 
Omnichannel Customer Experience
Omnichannel Customer ExperienceOmnichannel Customer Experience
Omnichannel Customer Experience
Divante
 
Ad

Similar to Go OO! - Real-life Design Patterns in PHP 5 (20)

OOP
OOPOOP
OOP
thinkphp
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
baabtra.com - No. 1 supplier of quality freshers
 
Best practices tekx
Best practices tekxBest practices tekx
Best practices tekx
Lorna Mitchell
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Henry Osborne
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
Toni Kolev
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
Tarek Mahmud Apu
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
Nate Abele
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Lorna Mitchell
 
Patterns in PHP
Patterns in PHPPatterns in PHP
Patterns in PHP
Diego Lewin
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
OOPs Concept
OOPs ConceptOOPs Concept
OOPs Concept
Mohammad Yousuf
 
Design Patterns and Usage
Design Patterns and UsageDesign Patterns and Usage
Design Patterns and Usage
Mindfire Solutions
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3
Jeremy Coates
 
PHP 5
PHP 5PHP 5
PHP 5
Rafael Corral
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
Nyros Technologies
 
Advanced php
Advanced phpAdvanced php
Advanced php
hamfu
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Ad

More from Stephan Schmidt (17)

Das Web Wird Mobil - Geolocation und Location Based Services
Das Web Wird Mobil - Geolocation und Location Based ServicesDas Web Wird Mobil - Geolocation und Location Based Services
Das Web Wird Mobil - Geolocation und Location Based Services
Stephan Schmidt
 
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
Stephan Schmidt
 
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
Stephan Schmidt
 
Continuous Integration mit Jenkins
Continuous Integration mit JenkinsContinuous Integration mit Jenkins
Continuous Integration mit Jenkins
Stephan Schmidt
 
Die Kunst des Software Design - Java
Die Kunst des Software Design - JavaDie Kunst des Software Design - Java
Die Kunst des Software Design - Java
Stephan Schmidt
 
PHP mit Paul Bocuse
PHP mit Paul BocusePHP mit Paul Bocuse
PHP mit Paul Bocuse
Stephan Schmidt
 
Der Erfolgreiche Programmierer
Der Erfolgreiche ProgrammiererDer Erfolgreiche Programmierer
Der Erfolgreiche Programmierer
Stephan Schmidt
 
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
Stephan Schmidt
 
Die Kunst Des Software Design
Die Kunst Des Software DesignDie Kunst Des Software Design
Die Kunst Des Software Design
Stephan Schmidt
 
Software-Entwicklung Im Team
Software-Entwicklung Im TeamSoftware-Entwicklung Im Team
Software-Entwicklung Im Team
Stephan Schmidt
 
JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5
Stephan Schmidt
 
Declarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHPDeclarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHP
Stephan Schmidt
 
XML-Socket-Server zur Kommunikation mit Flash
XML-Socket-Server zur Kommunikation mit FlashXML-Socket-Server zur Kommunikation mit Flash
XML-Socket-Server zur Kommunikation mit Flash
Stephan Schmidt
 
Interprozesskommunikation mit PHP
Interprozesskommunikation mit PHPInterprozesskommunikation mit PHP
Interprozesskommunikation mit PHP
Stephan Schmidt
 
PHP im High End
PHP im High EndPHP im High End
PHP im High End
Stephan Schmidt
 
Dynamische Websites mit XML
Dynamische Websites mit XMLDynamische Websites mit XML
Dynamische Websites mit XML
Stephan Schmidt
 
Web 2.0 Mit Der Yahoo User Interface Library
Web 2.0 Mit Der Yahoo User Interface LibraryWeb 2.0 Mit Der Yahoo User Interface Library
Web 2.0 Mit Der Yahoo User Interface Library
Stephan Schmidt
 
Das Web Wird Mobil - Geolocation und Location Based Services
Das Web Wird Mobil - Geolocation und Location Based ServicesDas Web Wird Mobil - Geolocation und Location Based Services
Das Web Wird Mobil - Geolocation und Location Based Services
Stephan Schmidt
 
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software Entwicklung in Teams wissen sollten
Stephan Schmidt
 
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten
Stephan Schmidt
 
Continuous Integration mit Jenkins
Continuous Integration mit JenkinsContinuous Integration mit Jenkins
Continuous Integration mit Jenkins
Stephan Schmidt
 
Die Kunst des Software Design - Java
Die Kunst des Software Design - JavaDie Kunst des Software Design - Java
Die Kunst des Software Design - Java
Stephan Schmidt
 
Der Erfolgreiche Programmierer
Der Erfolgreiche ProgrammiererDer Erfolgreiche Programmierer
Der Erfolgreiche Programmierer
Stephan Schmidt
 
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
23 Dinge, die Sie über Software-Entwicklung in Teams wissen sollten.
Stephan Schmidt
 
Die Kunst Des Software Design
Die Kunst Des Software DesignDie Kunst Des Software Design
Die Kunst Des Software Design
Stephan Schmidt
 
Software-Entwicklung Im Team
Software-Entwicklung Im TeamSoftware-Entwicklung Im Team
Software-Entwicklung Im Team
Stephan Schmidt
 
JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5
Stephan Schmidt
 
Declarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHPDeclarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHP
Stephan Schmidt
 
XML-Socket-Server zur Kommunikation mit Flash
XML-Socket-Server zur Kommunikation mit FlashXML-Socket-Server zur Kommunikation mit Flash
XML-Socket-Server zur Kommunikation mit Flash
Stephan Schmidt
 
Interprozesskommunikation mit PHP
Interprozesskommunikation mit PHPInterprozesskommunikation mit PHP
Interprozesskommunikation mit PHP
Stephan Schmidt
 
Dynamische Websites mit XML
Dynamische Websites mit XMLDynamische Websites mit XML
Dynamische Websites mit XML
Stephan Schmidt
 
Web 2.0 Mit Der Yahoo User Interface Library
Web 2.0 Mit Der Yahoo User Interface LibraryWeb 2.0 Mit Der Yahoo User Interface Library
Web 2.0 Mit Der Yahoo User Interface Library
Stephan Schmidt
 

Recently uploaded (20)

India’s Role in Supporting Nordic Innovation Through Global Capability Centers
India’s Role in Supporting Nordic Innovation Through Global Capability CentersIndia’s Role in Supporting Nordic Innovation Through Global Capability Centers
India’s Role in Supporting Nordic Innovation Through Global Capability Centers
Inductus GCC
 
Oleksandr Tsapenko: Чому Agile не працює? (UA)
Oleksandr Tsapenko: Чому Agile не працює? (UA)Oleksandr Tsapenko: Чому Agile не працює? (UA)
Oleksandr Tsapenko: Чому Agile не працює? (UA)
Lviv Startup Club
 
Market Dynamics Sample Report - Redacted
Market Dynamics Sample Report - RedactedMarket Dynamics Sample Report - Redacted
Market Dynamics Sample Report - Redacted
Mintel Group
 
process design and analysis oprations and supply chain management
process design and analysis oprations and supply chain managementprocess design and analysis oprations and supply chain management
process design and analysis oprations and supply chain management
nitinemba2318
 
Event Report - SAP Sapphire 2025 Orlando - Good work more to come
Event Report - SAP Sapphire 2025 Orlando - Good work more to comeEvent Report - SAP Sapphire 2025 Orlando - Good work more to come
Event Report - SAP Sapphire 2025 Orlando - Good work more to come
Holger Mueller
 
OwnAir - Your Cinema Everywhere | Business Plan
OwnAir - Your Cinema Everywhere | Business PlanOwnAir - Your Cinema Everywhere | Business Plan
OwnAir - Your Cinema Everywhere | Business Plan
Alessandro Masi
 
Harmony Projects (Pvt) Ltd Company Profile.pdf
Harmony Projects (Pvt) Ltd Company Profile.pdfHarmony Projects (Pvt) Ltd Company Profile.pdf
Harmony Projects (Pvt) Ltd Company Profile.pdf
Harmony Developments
 
Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)
Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)
Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)
Lviv Startup Club
 
Oleksandr Osypenko: Команда проєкту (UA)
Oleksandr Osypenko: Команда проєкту (UA)Oleksandr Osypenko: Команда проєкту (UA)
Oleksandr Osypenko: Команда проєкту (UA)
Lviv Startup Club
 
Best Financial and Banking Services in India.pptx
Best Financial and Banking Services in India.pptxBest Financial and Banking Services in India.pptx
Best Financial and Banking Services in India.pptx
workmintmedia
 
Europe Toys Market Size, Share, Trends & Report | 2034
Europe Toys Market Size, Share, Trends & Report | 2034Europe Toys Market Size, Share, Trends & Report | 2034
Europe Toys Market Size, Share, Trends & Report | 2034
GeorgeButtler
 
Automotive Filter Test ..pdf
Automotive Filter Test             ..pdfAutomotive Filter Test             ..pdf
Automotive Filter Test ..pdf
Test Master
 
A Brief Introduction About Gregory Felber
A Brief Introduction About Gregory FelberA Brief Introduction About Gregory Felber
A Brief Introduction About Gregory Felber
Gregory Felber
 
Summary of Comments on Conference 2 Notes for Car and Home Show.pdf
Summary of Comments on Conference 2 Notes for Car and Home Show.pdfSummary of Comments on Conference 2 Notes for Car and Home Show.pdf
Summary of Comments on Conference 2 Notes for Car and Home Show.pdf
Brij Consulting, LLC
 
How to Quickly Hire Java Developers for Java App Development and IT Outsourci...
How to Quickly Hire Java Developers for Java App Development and IT Outsourci...How to Quickly Hire Java Developers for Java App Development and IT Outsourci...
How to Quickly Hire Java Developers for Java App Development and IT Outsourci...
Mobisoft Infotech
 
Oleksandr Osypenko: Introduction to PMO (UA)
Oleksandr Osypenko: Introduction to PMO (UA)Oleksandr Osypenko: Introduction to PMO (UA)
Oleksandr Osypenko: Introduction to PMO (UA)
Lviv Startup Club
 
Water Pump Market Size, Share and Forecast | 2025-2034
Water Pump Market Size, Share and Forecast | 2025-2034Water Pump Market Size, Share and Forecast | 2025-2034
Water Pump Market Size, Share and Forecast | 2025-2034
GeorgeButtler
 
OwnAir - Your Cinema Everywhere | Pitch Deck
OwnAir - Your Cinema Everywhere | Pitch DeckOwnAir - Your Cinema Everywhere | Pitch Deck
OwnAir - Your Cinema Everywhere | Pitch Deck
Alessandro Masi
 
HVAC Filter Test .pdf
HVAC Filter Test                    .pdfHVAC Filter Test                    .pdf
HVAC Filter Test .pdf
Test Master
 
The Evolution of Down Proof Fabric in Fashion Design
The Evolution of Down Proof Fabric in Fashion DesignThe Evolution of Down Proof Fabric in Fashion Design
The Evolution of Down Proof Fabric in Fashion Design
Stk-Interlining
 
India’s Role in Supporting Nordic Innovation Through Global Capability Centers
India’s Role in Supporting Nordic Innovation Through Global Capability CentersIndia’s Role in Supporting Nordic Innovation Through Global Capability Centers
India’s Role in Supporting Nordic Innovation Through Global Capability Centers
Inductus GCC
 
Oleksandr Tsapenko: Чому Agile не працює? (UA)
Oleksandr Tsapenko: Чому Agile не працює? (UA)Oleksandr Tsapenko: Чому Agile не працює? (UA)
Oleksandr Tsapenko: Чому Agile не працює? (UA)
Lviv Startup Club
 
Market Dynamics Sample Report - Redacted
Market Dynamics Sample Report - RedactedMarket Dynamics Sample Report - Redacted
Market Dynamics Sample Report - Redacted
Mintel Group
 
process design and analysis oprations and supply chain management
process design and analysis oprations and supply chain managementprocess design and analysis oprations and supply chain management
process design and analysis oprations and supply chain management
nitinemba2318
 
Event Report - SAP Sapphire 2025 Orlando - Good work more to come
Event Report - SAP Sapphire 2025 Orlando - Good work more to comeEvent Report - SAP Sapphire 2025 Orlando - Good work more to come
Event Report - SAP Sapphire 2025 Orlando - Good work more to come
Holger Mueller
 
OwnAir - Your Cinema Everywhere | Business Plan
OwnAir - Your Cinema Everywhere | Business PlanOwnAir - Your Cinema Everywhere | Business Plan
OwnAir - Your Cinema Everywhere | Business Plan
Alessandro Masi
 
Harmony Projects (Pvt) Ltd Company Profile.pdf
Harmony Projects (Pvt) Ltd Company Profile.pdfHarmony Projects (Pvt) Ltd Company Profile.pdf
Harmony Projects (Pvt) Ltd Company Profile.pdf
Harmony Developments
 
Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)
Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)
Oleksandr Osypenko: Вступ до PMP та огляд курсу (UA)
Lviv Startup Club
 
Oleksandr Osypenko: Команда проєкту (UA)
Oleksandr Osypenko: Команда проєкту (UA)Oleksandr Osypenko: Команда проєкту (UA)
Oleksandr Osypenko: Команда проєкту (UA)
Lviv Startup Club
 
Best Financial and Banking Services in India.pptx
Best Financial and Banking Services in India.pptxBest Financial and Banking Services in India.pptx
Best Financial and Banking Services in India.pptx
workmintmedia
 
Europe Toys Market Size, Share, Trends & Report | 2034
Europe Toys Market Size, Share, Trends & Report | 2034Europe Toys Market Size, Share, Trends & Report | 2034
Europe Toys Market Size, Share, Trends & Report | 2034
GeorgeButtler
 
Automotive Filter Test ..pdf
Automotive Filter Test             ..pdfAutomotive Filter Test             ..pdf
Automotive Filter Test ..pdf
Test Master
 
A Brief Introduction About Gregory Felber
A Brief Introduction About Gregory FelberA Brief Introduction About Gregory Felber
A Brief Introduction About Gregory Felber
Gregory Felber
 
Summary of Comments on Conference 2 Notes for Car and Home Show.pdf
Summary of Comments on Conference 2 Notes for Car and Home Show.pdfSummary of Comments on Conference 2 Notes for Car and Home Show.pdf
Summary of Comments on Conference 2 Notes for Car and Home Show.pdf
Brij Consulting, LLC
 
How to Quickly Hire Java Developers for Java App Development and IT Outsourci...
How to Quickly Hire Java Developers for Java App Development and IT Outsourci...How to Quickly Hire Java Developers for Java App Development and IT Outsourci...
How to Quickly Hire Java Developers for Java App Development and IT Outsourci...
Mobisoft Infotech
 
Oleksandr Osypenko: Introduction to PMO (UA)
Oleksandr Osypenko: Introduction to PMO (UA)Oleksandr Osypenko: Introduction to PMO (UA)
Oleksandr Osypenko: Introduction to PMO (UA)
Lviv Startup Club
 
Water Pump Market Size, Share and Forecast | 2025-2034
Water Pump Market Size, Share and Forecast | 2025-2034Water Pump Market Size, Share and Forecast | 2025-2034
Water Pump Market Size, Share and Forecast | 2025-2034
GeorgeButtler
 
OwnAir - Your Cinema Everywhere | Pitch Deck
OwnAir - Your Cinema Everywhere | Pitch DeckOwnAir - Your Cinema Everywhere | Pitch Deck
OwnAir - Your Cinema Everywhere | Pitch Deck
Alessandro Masi
 
HVAC Filter Test .pdf
HVAC Filter Test                    .pdfHVAC Filter Test                    .pdf
HVAC Filter Test .pdf
Test Master
 
The Evolution of Down Proof Fabric in Fashion Design
The Evolution of Down Proof Fabric in Fashion DesignThe Evolution of Down Proof Fabric in Fashion Design
The Evolution of Down Proof Fabric in Fashion Design
Stk-Interlining
 

Go OO! - Real-life Design Patterns in PHP 5

  • 1. Stephan Schmidt, 1&1 Internet AG Go OO! Real-Life Design Patterns in PHP5
  • 2. Agenda What are Design Patterns? PHP5's new OO features Creational Patterns Structural Patterns Behavioral Patterns A Look at SPL Real-Life Appliances
  • 3. The speaker Working for 1&1 Internet AG Founding member of PHP Application Tools (pat) Active developer of the PEAR community Writing for several PHP magazines
  • 4. What are Design Patterns? standard solutions to common problems in software architecture independent of the programming language ease the communication between the developers typically describe the interaction of classes and objects
  • 5. PHP5's new OO features OO-model has been completely revamped objects are not any longer just &quot;improved arrays&quot; new SPL extension
  • 6. Pass-by-reference class Foo { var $val = 'Foo'; } $foo = new Foo(); $bar = $foo; $bar->val = 'Bar'; echo $bar->val . &quot;\n&quot;; echo $foo->val . &quot;\n&quot;; Bar Foo Bar Bar PHP 5 PHP 4
  • 7. Constructors/Destructors Constructor is named __construct() Destructor is named __destruct() class Foo { public function __construct() { print &quot;Foo created\n&quot;; } public function __destruct() { print &quot;Foo destroyed\n&quot;; } } $bar = new Foo(); unset($bar);
  • 8. Visibility Restrict access to properties and methods of a class. public := all protected := class and descendants private := only the class
  • 9. Visibility (2): Example class Foo { private $foo = 'foo'; public $bar = 'bar'; protected $tomato = 'tomato'; } $bar = new Foo(); print &quot;{$bar->bar}\n&quot;; print &quot;{$bar->foo}\n&quot;; $ php visibility.php bar Fatal error: Cannot access private property Foo::$foo in /home/schst/go-oo/visibility.php on line 9
  • 10. Static methods/properties Available outside of the object context can be public/private/protected self::$foo instead of $this->foo class Foo { public static $foo = 'bar'; public static function getFoo() { return self::$foo; } } print Foo::$foo . &quot;\n&quot;; print Foo::getFoo() . &quot;\n&quot;;
  • 11. Object Abstraction Abstract classes cannot be instantiated abstract class AbstractClass { abstract public function doSomething(); } class ConcreteClass extends AbstractClass { public function doSomething() { print &quot;I've done something.\n&quot;; } } $foo = new ConcreteClass(); $bar = new AbstractClass(); Fatal error: Cannot instantiate abstract class AbstractClass in ... on line 11
  • 12. Interfaces Specify the methods a class has to implement without defining how they are implemented Classes may implement more than one interface If a class does not implement all methods of an interface => E_FATAL
  • 13. Interfaces (2): Example interface IRequest { public function getValue($name); public function getHeader($name); } class HttpRequest implements IRequest { public function getValue($name) { return $_REQUEST[$name]; } } Fatal error: Class HttpRequest contains 1 abstract methods and must therefore be declared abstract (IRequest::getHeader) in /home/schst/go-oo/interfaces.php on line 10
  • 14. Property Overloading Intercept access to properties, that do not exist class Foo { private $props = array('foo' => 'bar'); public function __get($prop) { if (!isset($this->props[$prop])) { return null; } return $this->props[$prop]; } } $foo = new Foo(); print &quot;{$foo->foo}\n&quot;;
  • 15. Method Overloading Intercept calls to methods, that do not exist class Foo { public function __call($method, $args) { if (is_callable($method)) { return call_user_func_array($method, $args); } return null; } } $foo = new Foo(); print $foo->strrev('tomato') . &quot;\n&quot;;
  • 16. __toString() Change string-cast behavior of an object class User { private $id; private $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function __toString() { return &quot;{$this->name} ({$this->id})\n&quot;; } } $schst = new User('schst', 'Stephan Schmidt'); print $schst;
  • 17. Object Iteration Objects can be used in foreach-statements class PearDevelopers { public $schst = 'Stephan Schmidt'; public $luckec = 'Carsten Lucke'; } $users = new PearDevelopers(); foreach ($users as $id => $name) { print &quot;$id is $name\n&quot;; } schst is Stephan Schmidt luckec is Carsten Lucke
  • 18. Misc additions Object cloning $bar = clone $foo; Type hints for objects public function foo (MyClass $obj) {} Exception Handling Reflection API __autoload() SPL
  • 19. Creational Patterns Create objects Hide the creational process Your application does not need to know, how the object has to be created Allows you to hide the concrete implementation Dynamic object configuration
  • 20. Factory Method Create objects based on some input Hides the object creation PEAR makes heavy use of this, like in PEAR::DB $con = DB::connect('mysql://user:pass@host/db'); The returned object is an instance of a subclass of DB_common based on the database type
  • 21. Singleton Makes sure that only one instance of a class exists class Registry { private static $instance = null; private function __construct() {} public $foo; public function singleton() { if (is_null(self::$instance)) { self::$instance = new Registry(); } return self::$instance; } }
  • 22. Singleton (2): Usage Commonly used for Configurations / Registries Sharing DB connections (combine it with factory method) Request objects … $reg1 = Registry::singleton(); $reg2 = Registry::singleton(); $reg1->foo = 'Bar'; print $reg2->foo . &quot;\n&quot;;
  • 23. Structural Patterns Define the relationships between classes and/or objects Object-composition Often use inheritance and interfaces
  • 24. Decorator Allows you to dynamically add functionality to an object Used for functionality that is used only in some cases Often used to avoid inheritance or when inheritance is not possible Decorator wraps the original objects
  • 25. Decorator (2): Component class String { private $string = null; public function __construct($string) { $this->string = $string; } public function __toString() { return $this->string; } public function getLength() { return strlen($this->string); } public function getString() { return $this->string; } public function setString($string) { $this->string = $string; } }
  • 26. Decorator (3): Abstract An abstract decorator abstract class String_Decorator { protected $obj; public function __construct($obj) { $this->obj = $obj; } public function __call($method, $args) { if (!method_exists($this->obj, $method)) { throw new Exception('Unknown method called.'); } return call_user_func_array( array($this->obj, $method), $args); } }
  • 27. Decorator (4): Bold Concrete Decorator class String_Decorator_Bold extends String_Decorator { public function __toString() { return '<b>' . $this->obj->__toString() . '</b>'; } } Usage $str = new String('Decorators are cool'); $strBold = new String_Decorator_Bold($str); print $strBold;
  • 28. Decorator (5): Reverse Adding a method class String_Decorator_Reverse extends String_Decorator { public function reverse(){ $str = $this->obj->getString(); $this->obj->setString(strrev($str)); } } Usage $str = new String('Decorators are cool'); $strRev = new String_Decorator_Reverse($str); $strRev->reverse(); print $strRev;
  • 29. Decorator (6): Combination Combining decorators $str = new String('Decorators are cool'); $strBold = new String_Decorator_Bold($str); $strRev = new String_Decorator_Reverse($strBold); $strRev->reverse(); print $strRev;
  • 30. Proxy Provides a placeholder for an objects to control or access this objects Very common with webservices or application servers $client = new SoapClient( 'http://api.google.com/GoogleSearch.wsdl'); $result = $client->doGoogleSearch(…);
  • 31. Proxy (2): Implementation Catch all method calls on the Proxy using __call() class Proxy { public function __construct() { // establish connection to the original object } public function __call($method, $args) { // forward the call to the original object // using any protocol you need } }
  • 32. Delegator Similar to Proxy Often referred to as &quot;Chained Proxy&quot; Acts as a proxy to several objects Intercept calls to all unknown methods and forward those to any of the delegates, that provides these methods Ready-to-use implementation for PHP: PEAR_Delegator
  • 33. Delegator (2): Example The Delegator class Foo extends PEAR_Delegator { public function __construct() { parent::_construct(); } public function __destruct() { parent::__destruct(); } public function displayFoo() { print &quot;foo\n&quot;; } }
  • 34. Delegator (3): Example The Delegates class Delegate1 { public function displayBar() { print &quot;bar\n&quot;; } } class Delegate2 { public function displayTomato() { print &quot;tomato\n&quot;; } }
  • 35. Delegator (4): Example Usage $delegator = new Foo(); $delegate1 = new Delegate1(); $delegate2 = new Delegate2(); $delegator->addDelegate($delegate1); $delegator->addDelegate($delegate2); $delegator->displayFoo(); $delegator->displayBar(); $delegator->displayTomato();
  • 36. Behavioral patterns Provide solutions for communication between objects Increase the flexibility of the communication
  • 37. Observer Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. First step to event-based development Often used for logging techniques
  • 38. Observer (2): Subject class Subject { private $observers = array(); public $state = null; public function attach(Observer $observer) { $this->observers[] = $observer; } public function detach(Observer $observer) { // remove the observer } public function notify() { for ($i = 0; $i < count($this->observers); $i++) { $this->observers[$i]->update(); } } }
  • 39. Observer (3): Observer class Observer { private $subject; private $name public function __construct($subject, $name) { $this->subject = $subject; $this->name = $name; } public function update() { $state = $this->subject->state; print $this->name.&quot;: State of subject is $state\n&quot;; } }
  • 40. Observer (4): Usage $subj = new Subject(); $ob1 = new Observer($subj, 'Observer 1'); $ob2 = new Observer($subj, 'Observer 2'); $subj->attach($ob1); $subj->attach($ob2); $subj->state = &quot;authenticated&quot;; $subj->notify();
  • 41. Standard PHP Library Bundled with PHP 5 and enabled by default Collection of interfaces Iterators ArrayAccess, Countable Subject/Observer (PHP 5.1) Collection of Classes Iterators Exceptions, FileObject (PHP 5.1)
  • 42. ArrayAccess Allows you to access any object as it were an array Interface provided by SPL public function offsetExists($offset); public function offsetGet($offset); public function offsetSet($offset, $value); public function offsetUnset($offset); Only works with [] array syntax
  • 43. ArrayAccess (2): Example class Foo implements ArrayAccess { private $props = array('foo' => 'Bar'); public function offsetExists($offset) { return isset($this->props[$offset]); } public function offsetGet($offset) { return $this->props[$offset]; } public function offsetSet($offset, $value) { $this->props[$offset] = $value; } public function offsetUnset($offset) { unset($this->props[$offset]); } }
  • 44. ArrayAccess (3): Example $obj = new Foo(); print $obj['foo'] . &quot;\n&quot;; $obj['bar'] = 3452; if (isset($obj['bar'])) { print $obj['bar'] . &quot;\n&quot;; } $ php arrayAccess.php Bar 3452
  • 45. Abstracting HTTP Requests Create request object to access request properties Replaces $_GET, $_POST, $_SERVER High level of abstraction Provides flexibility Implements ArrayAccess interface to allow $request['property'] syntax for a shallow learning curve
  • 46. Request (2): Example abstract class Request implements ArrayAccess { protected $properties = array(); public function offsetExists($offset) { return isset($this->properties[$offset]); } public function offsetGet($offset) { return $this->properties[$offset]; } public function offsetSet($offset, $value) { $this->properties[$offset] = $value; } public function offsetUnset($offset) { unset($this->properties[$offset]); } }
  • 47. Request (3): HTTP class Request_HTTP extends Request { public function __construct() { $this->properties = $_REQUEST; } } $request = new Request_HTTP(); if (isset($request['foo'])) { echo $request['foo']; } else { echo &quot;property foo has not been set&quot;; } http://www.example.com/?foo=bar
  • 48. Replacing the Request Request_HTTP can be replaced by any class with the same interface Request_CLI Request_SOAP Combine this with singleton and factory method: $request = Request::get('HTTP');
  • 49. Request (4): CLI class Request_CLI extends Request { public function __construct() { array_shift($_SERVER['argv']); foreach ($_SERVER['argv'] as $pair) { list($key, $value) = explode('=', $pair); $this->properties[$key] = $value; } } } $request = new Request_CLI(); if (isset($request['foo'])) { echo $request['foo']; } else { echo &quot;property foo has not been set&quot;; } $ ./script.php foo=bar
  • 50. Intercepting filters Allows you to preprocess the request data Apply centralized authentication mechanism Validate/modify request properties Forward based on the URI Borrowed from J2EE Servlets
  • 51. Intercepting filters (2) abstract class Request implements ArrayAccess { … protected $filters = array(); public function addFilter(InterceptingFilter $filter) { $this->filters[] = $filter; } protected function applyFilters() { for ($i = 0; $i < $this->filters; $i++) { $this->filters[$i]->doFilter($this); } } } Changes to Request
  • 52. Intercepting filters (3) Simple filter interface class Request_HTTP extends Request { public function __construct() { $this->properties = $_REQUEST; $this->applyFilters(); } } interface InterceptingFilter { public function doFilter(Request $request); } Changes to Request_HTTP
  • 53. Iterators Interface that allows you to influence the way foreach-iterates over an object mixed public function current(); mixed public function key(); void public function next(); boolean public function valid(); void public function rewind(); Traverse any data using foreach (dir listing, array, text file, etc.)
  • 54. Iterators (2): Example class CSVFile implements Iterator { protected $file; protected $fp; protected $line; protected $key = -1; public function __construct($file) { $this->file = $file; $this->fp = @fopen($this->file, 'r'); if (!$this->fp) { throw new Exception('Could not open file.'); } } public function __desctruct() { fclose($this->fp); }
  • 55. Iterators (3): Example cont. public function next(){ if (!feof($this->fp)) { $this->key++; $this->line = fgetcsv($this->fp); $this->valid = true; } else { $this->valid = false; } } public function rewind() { $this->key = -1; fseek($this->fp, 0); $this->next(); } }
  • 56. Iterators (4): Example cont. public function current() { return $this->line; } public function key() { return $this->key; } public function valid() { return $this->valid; } }
  • 57. Iterators (5): Example cont. $csvFile = new CSVFile('users.csv'); foreach ($csvFile as $entry) { print_r($entry); } Array ( [0] => Array ( [0] => 'schst', [1] => 'Stephan Schmidt' ), [1] => Array ( [0] => 'luckec', [1] => 'Carsten Lucke' ), )
  • 58. Recursive Iterators Extends the Iterator interface boolean public function hasChildren(); Iterator public function getChildren(); getChildren() returns an object that implements Iterator Traverse hierarchical data using the class RecursiveIteratorIterator
  • 59. Abstracting data structures Iterators allow you to abstract hierarchical structures in your application using foreach. The user of the data does not know how it is computed, but only traverses it. Simple example: Page definition files in a MVC-framework.
  • 60. Example: Page defintions One page per configuration file: title = &quot;Homepage&quot; desc = &quot;This is the homepage&quot; class = &quot;Homepage&quot; Navigation structure in the filesystem: index.ini projects.ini projects/ pat.ini pear.ini pear/ services_ebay.ini xml_serializer.ini
  • 61. Example: Page Class class Page { public $name; public $title; public $desc; public function __construct($basePath, $name) { $fname = $basePath . '/' . $name . '.ini'; $tmp = parse_ini_file($fname); $this->name = $name; $this->title = $tmp['title']; $this->desc = $tmp['desc']; } } $home = new Page('pages', 'index'); print $home->title;
  • 62. Example: Sitemap Class class Sitemap implements Iterator { protected $path; protected $pos = 0; protected $pages = array(); public function __construct($path) { $this->path = $path; if (file_exists($this->path)) { $dir = dir($path); while ($entry = $dir->read()) { $this->pages[] = new Page($this->path, $entry); } } } …
  • 63. Example: Sitemap Class (2) public function current() { return $this->pages[$this->pos]; } public function key() { return $this->pos; } public function next() { ++$this->pos; } public function rewind() { $this->pos = 0; } public function valid() { return isset($this->pages[$this->pos]); } }
  • 64. Example: Sitemap Usage User does not need to know when and how the page objects are created and in which order they are traversed. INI files can be substituted with XML, content from a database, etc. $sitemap = new Sitemap('pages'); foreach ($sitemap as $page) { echo $page->title . &quot;<br />\n&quot;; }
  • 65. Example: Going recursive class Page extends Sitemap { … public function __construct($basePath, $name) { $fname = $basePath . '/' . $name . '.ini'; $tmp = parse_ini_file($fname); $this->name = $name; $this->title = $tmp['title']; $this->desc = $tmp['desc']; $subPath = $basePath . '/' . $this->name; parent::__construct($subPath); } public function hasPages() { return !empty($this->pages); } }
  • 66. Example: Going recursive Restricted to two levels :( Not intuitive $sitemap = new Sitemap('pages'); foreach ($sitemap as $page) { echo $page->title . '<br />'; foreach ($page as $subPage) { echo ' - ' . $subPage->title . '<br />'; } }
  • 67. Example: Going recursive class Sitemap implements RecursiveIterator { … public function hasChildren() { return $this->pages[$this->pos]->hasPages(); } public function getChildren() { return $this->pages[$this->pos]; } }
  • 68. Example: Done $sitemap = new Sitemap('pages'); $iterator = new RecursiveIteratorIterator($sitemap, RIT_SELF_FIRST); foreach ($iterator as $page) { $depth = $iterator->getDepth(); if ($depth > 0) { echo str_repeat('&nbsp;', $depth*2) . ' - '; } echo $page->title . '<br />'; } Homepage Projects - PAT-Projects - PEAR-Projects - Services_Ebay - XML_Serializer
  • 69. Useful Resources Design Patterns http://www.dofactory.com/Patterns/Patterns.aspx phpPatterns() http://www.phppatterns.com J2EE Patterns http://java.sun.com/blueprints/corej2eepatterns/ Microsoft patterns http://msdn.microsoft.com/architecture/patterns/
  • 70. The end Thank you for your attention! ANY QUESTIONS ? [email_address] http://www.php-tools.net Stephan Schmidt, 1&1 Internet AG