SlideShare a Scribd company logo
1 of 70
Stephan Schmidt, 1&1 Internet AG Go OO! Real-Life Design Patterns in PHP5
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The speaker ,[object Object],[object Object],[object Object],[object Object]
What are Design Patterns? ,[object Object],[object Object],[object Object],[object Object]
PHP5's new OO features ,[object Object],[object Object],[object Object]
Pass-by-reference class Foo { var $val = 'Foo'; } $foo = new Foo(); $bar = $foo; $bar->val = 'Bar'; echo $bar->val . ""; echo $foo->val . ""; Bar Foo Bar Bar PHP 5 PHP 4
Constructors/Destructors ,[object Object],[object Object],class Foo { public function __construct() { print "Foo created"; } public function __destruct() { print "Foo destroyed"; } } $bar = new Foo(); unset($bar);
Visibility ,[object Object],[object Object],[object Object],[object Object]
Visibility (2): Example class Foo { private  $foo = 'foo'; public  $bar = 'bar'; protected $tomato = 'tomato'; } $bar = new Foo(); print "{$bar->bar}"; print "{$bar->foo}"; $ 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 ,[object Object],[object Object],[object Object],class Foo { public static $foo = 'bar'; public static function getFoo() { return self::$foo; } } print Foo::$foo . ""; print Foo::getFoo() . "";
Object Abstraction ,[object Object],abstract class AbstractClass { abstract public function doSomething(); } class ConcreteClass extends AbstractClass { public function doSomething() { print "I've done something."; } } $foo = new ConcreteClass(); $bar = new AbstractClass(); Fatal error: Cannot instantiate abstract class AbstractClass in ... on line 11
Interfaces ,[object Object],[object Object],[object Object]
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 ,[object Object],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}";
Method Overloading ,[object Object],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') . "";
__toString() ,[object 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})"; } } $schst = new User('schst', 'Stephan Schmidt'); print $schst;
Object Iteration ,[object Object],class PearDevelopers { public $schst  = 'Stephan Schmidt'; public $luckec = 'Carsten Lucke'; } $users = new PearDevelopers(); foreach ($users as $id => $name) { print "$id is $name"; } schst is Stephan Schmidt luckec is Carsten Lucke
Misc additions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creational Patterns ,[object Object],[object Object],[object Object],[object Object],[object Object]
Factory Method ,[object Object],[object Object],[object Object],$con = DB::connect('mysql://user:pass@host/db'); ,[object Object]
Singleton ,[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],$reg1 = Registry::singleton(); $reg2 = Registry::singleton(); $reg1->foo = 'Bar'; print $reg2->foo . "";
Structural Patterns ,[object Object],[object Object],[object Object]
Decorator ,[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],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 ,[object Object],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 ,[object Object],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 ,[object Object],$str  = new String('Decorators are cool'); $strBold = new String_Decorator_Bold($str); $strRev  = new String_Decorator_Reverse($strBold); $strRev->reverse(); print $strRev;
Proxy ,[object Object],[object Object],$client = new SoapClient( 'http://api.google.com/GoogleSearch.wsdl'); $result = $client->doGoogleSearch(…);
Proxy (2): Implementation ,[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Delegator (2): Example ,[object Object],class Foo extends PEAR_Delegator { public function __construct() { parent::_construct(); } public function __destruct() { parent::__destruct(); } public function displayFoo() { print &quot;foo&quot;; } }
Delegator (3): Example ,[object Object],class Delegate1 { public function displayBar() { print &quot;bar&quot;; } } class Delegate2 { public function displayTomato() { print &quot;tomato&quot;; } }
Delegator (4): Example ,[object Object],$delegator = new Foo(); $delegate1 = new Delegate1(); $delegate2 = new Delegate2(); $delegator->addDelegate($delegate1); $delegator->addDelegate($delegate2); $delegator->displayFoo(); $delegator->displayBar(); $delegator->displayTomato();
Behavioral patterns ,[object Object],[object Object]
Observer ,[object Object],[object Object],[object Object]
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&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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
ArrayAccess ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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;&quot;; $obj['bar'] = 3452; if (isset($obj['bar'])) { print $obj['bar'] . &quot;&quot;; } $ php arrayAccess.php Bar 3452
Abstracting HTTP Requests ,[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],$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 ,[object Object],[object Object],[object Object],[object Object],[object Object]
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) ,[object Object],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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Abstracting data structures ,[object Object],[object Object],[object Object]
Example: Page defintions ,[object Object],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 ,[object Object],[object Object],$sitemap = new Sitemap('pages'); foreach ($sitemap as $page) { echo $page->title . &quot;<br />&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 ,[object Object],[object Object],$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 ,[object Object],[object Object],[object Object],[object Object]
The end ,[object Object],[object Object],[object Object],[object Object],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
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
XML and PHP 5
XML and PHP 5XML and PHP 5
XML and PHP 5
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
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
 
lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 

Viewers also liked

Using spl tools in your code
Using spl tools in your codeUsing spl tools in your code
Using spl tools in your codeElizabeth 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 MagentoDivante
 
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 ScientistsDavid De Roure
 
Software design principles for evolving architectures
Software design principles for evolving architecturesSoftware design principles for evolving architectures
Software design principles for evolving architecturesFirat 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.3Fabien Potencier
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
Base SAS Exam Questions
Base SAS Exam QuestionsBase SAS Exam Questions
Base SAS Exam Questionsguestc45097
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSergey 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 eBusinessDivante
 
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 ExperienceDivante
 

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
 
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)
 
Refactoring
RefactoringRefactoring
Refactoring
 
Design patterns in Magento
Design patterns in MagentoDesign patterns in Magento
Design patterns in Magento
 
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
 
Software design principles for evolving architectures
Software design principles for evolving architecturesSoftware design principles for evolving architectures
Software design principles for evolving architectures
 
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
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Base SAS Exam Questions
Base SAS Exam QuestionsBase SAS Exam Questions
Base SAS Exam Questions
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
 
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
 
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)
 
Omnichannel Customer Experience
Omnichannel Customer ExperienceOmnichannel Customer Experience
Omnichannel Customer Experience
 

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

Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
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 OOPWildan Maulana
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPAlena Holligan
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolveXSolve
 
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.3Jeremy Coates
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpointwebhostingguy
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closuresmelechi
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmersAlexander Varwijk
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibilitymachuga
 
Giới thiệu PHP 7
Giới thiệu PHP 7Giới thiệu PHP 7
Giới thiệu PHP 7ZendVN
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)James Titcumb
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Jacopo Romei
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...James Titcumb
 

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

Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
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
 
OOP
OOPOOP
OOP
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
PHP
PHP PHP
PHP
 
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
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
PHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & ClosuresPHP 5.3 Part 2 - Lambda Functions & Closures
PHP 5.3 Part 2 - Lambda Functions & Closures
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Giới thiệu PHP 7
Giới thiệu PHP 7Giới thiệu PHP 7
Giới thiệu PHP 7
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
Oops in php
Oops in phpOops in php
Oops in php
 
Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011Symfony CMF - PHP Conference Brazil 2011
Symfony CMF - PHP Conference Brazil 2011
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 

More from 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 ServicesStephan 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 solltenStephan 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 solltenStephan Schmidt
 
Continuous Integration mit Jenkins
Continuous Integration mit JenkinsContinuous Integration mit Jenkins
Continuous Integration mit JenkinsStephan Schmidt
 
Die Kunst des Software Design - Java
Die Kunst des Software Design - JavaDie Kunst des Software Design - Java
Die Kunst des Software Design - JavaStephan Schmidt
 
Der Erfolgreiche Programmierer
Der Erfolgreiche ProgrammiererDer Erfolgreiche Programmierer
Der Erfolgreiche ProgrammiererStephan 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 DesignStephan Schmidt
 
Software-Entwicklung Im Team
Software-Entwicklung Im TeamSoftware-Entwicklung Im Team
Software-Entwicklung Im TeamStephan 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 5Stephan Schmidt
 
Declarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHPDeclarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHPStephan 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 FlashStephan Schmidt
 
Interprozesskommunikation mit PHP
Interprozesskommunikation mit PHPInterprozesskommunikation mit PHP
Interprozesskommunikation mit PHPStephan Schmidt
 
Dynamische Websites mit XML
Dynamische Websites mit XMLDynamische Websites mit XML
Dynamische Websites mit XMLStephan 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 LibraryStephan Schmidt
 

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
 
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
 
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
 
Continuous Integration mit Jenkins
Continuous Integration mit JenkinsContinuous Integration mit Jenkins
Continuous Integration mit Jenkins
 
Die Kunst des Software Design - Java
Die Kunst des Software Design - JavaDie Kunst des Software Design - Java
Die Kunst des Software Design - Java
 
PHP mit Paul Bocuse
PHP mit Paul BocusePHP mit Paul Bocuse
PHP mit Paul Bocuse
 
Der Erfolgreiche Programmierer
Der Erfolgreiche ProgrammiererDer Erfolgreiche Programmierer
Der Erfolgreiche Programmierer
 
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.
 
Die Kunst Des Software Design
Die Kunst Des Software DesignDie Kunst Des Software Design
Die Kunst Des Software Design
 
Software-Entwicklung Im Team
Software-Entwicklung Im TeamSoftware-Entwicklung Im Team
Software-Entwicklung Im Team
 
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
 
Declarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHPDeclarative Development Using Annotations In PHP
Declarative Development Using Annotations In PHP
 
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
 
Interprozesskommunikation mit PHP
Interprozesskommunikation mit PHPInterprozesskommunikation mit PHP
Interprozesskommunikation mit PHP
 
PHP im High End
PHP im High EndPHP im High End
PHP im High End
 
Dynamische Websites mit XML
Dynamische Websites mit XMLDynamische Websites mit XML
Dynamische Websites mit XML
 
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
 

Recently uploaded

FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607dollysharma2066
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCRashishs7044
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCRashishs7044
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Seta Wicaksana
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessSeta Wicaksana
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,noida100girls
 
Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03DallasHaselhorst
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...lizamodels9
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchirictsugar
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607dollysharma2066
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdfKhaled Al Awadi
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...lizamodels9
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menzaictsugar
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...ictsugar
 
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckPitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckHajeJanKamps
 

Recently uploaded (20)

FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
 
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Greater Noida ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
 
8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR8447779800, Low rate Call girls in Saket Delhi NCR
8447779800, Low rate Call girls in Saket Delhi NCR
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful Business
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
BEST Call Girls In Old Faridabad ✨ 9773824855 ✨ Escorts Service In Delhi Ncr,
 
Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03Cybersecurity Awareness Training Presentation v2024.03
Cybersecurity Awareness Training Presentation v2024.03
 
Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)
 
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
Call Girls In Radisson Blu Hotel New Delhi Paschim Vihar ❤️8860477959 Escorts...
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 
Marketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent ChirchirMarketplace and Quality Assurance Presentation - Vincent Chirchir
Marketplace and Quality Assurance Presentation - Vincent Chirchir
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
 
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdfNewBase  19 April  2024  Energy News issue - 1717 by Khaled Al Awadi.pdf
NewBase 19 April 2024 Energy News issue - 1717 by Khaled Al Awadi.pdf
 
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
Lowrate Call Girls In Sector 18 Noida ❤️8860477959 Escorts 100% Genuine Servi...
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
 
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckPitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
 

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.
  • 3.
  • 4.
  • 5.
  • 6. Pass-by-reference class Foo { var $val = 'Foo'; } $foo = new Foo(); $bar = $foo; $bar->val = 'Bar'; echo $bar->val . &quot;&quot;; echo $foo->val . &quot;&quot;; Bar Foo Bar Bar PHP 5 PHP 4
  • 7.
  • 8.
  • 9. Visibility (2): Example class Foo { private $foo = 'foo'; public $bar = 'bar'; protected $tomato = 'tomato'; } $bar = new Foo(); print &quot;{$bar->bar}&quot;; print &quot;{$bar->foo}&quot;; $ php visibility.php bar Fatal error: Cannot access private property Foo::$foo in /home/schst/go-oo/visibility.php on line 9
  • 10.
  • 11.
  • 12.
  • 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.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 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.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 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&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.
  • 42.
  • 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;&quot;; $obj['bar'] = 3452; if (isset($obj['bar'])) { print $obj['bar'] . &quot;&quot;; } $ php arrayAccess.php Bar 3452
  • 45.
  • 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.
  • 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.
  • 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.
  • 53.
  • 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.
  • 59.
  • 60.
  • 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.
  • 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.
  • 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.
  • 70.