Understanding the PHP Object Model

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

3 comments

Comments 1 - 3 of 3 previous next Post a comment

  • + steina steina 2 years ago
    May it be possbile to download the presentation? private messages are disabled .. thx :)
  • + antonic74pa antonic74pa 2 years ago
    Good
  • + kivancerten kivancerten 2 years ago
    For beginners to PHP Object Model, very good slides, thanks...
Post a comment
Embed Video
Edit your comment Cancel

Notes on slide 1

Theme created by Sakari Koivunen and Henrik Omma Released under the LGPL license.

108 Favorites & 2 Groups

Understanding the PHP Object Model - Presentation Transcript

  1. Welcome! Understanding the PHP Object Model Sebastian Bergmann http://sebastian-bergmann.de/ May 22 nd 2008
  2. Who I am
    • Sebastian Bergmann
    • Involved in the PHP Project since 2000
    • Developer of PHPUnit
    • Author, Consultant, Coach, Trainer
  3. Outline
    • Before we can “dive into” the object model of PHP, we need to understand the basic concepts of object-orientation
  4. Outline
    • Before we can “dive into” the object model of PHP, we need to understand the basic concepts of object-orientation
    • Then we can look at how PHP implements these concepts
  5. Outline
    • Before we can “dive into” the object model of PHP, we need to understand the basic concepts of object-orientation
    • Then we can look at how PHP implements these concepts
    • Finally, we can look at what PHP supports in addition to the basic concepts of object-orientation
  6. Introduction
    • Answer #1 (by Timothy A. Budd)
    What is Object-Orientation?
  7. Introduction
    • Answer #1 (by Timothy A. Budd)
      • Everything is an object
    What is Object-Orientation?
  8. Introduction
    • Answer #1 (by Timothy A. Budd)
      • Everything is an object
      • Objects communicate by sending messages
    What is Object-Orientation?
  9. Introduction
    • Answer #1 (by Timothy A. Budd)
      • Everything is an object
      • Objects communicate by sending messages
      • Objects have private state
    What is Object-Orientation?
  10. Introduction
    • Answer #1 (by Timothy A. Budd)
      • Everything is an object
      • Objects communicate by sending messages
      • Objects have private state
      • Every object is an instance of one class
    What is Object-Orientation?
  11. Introduction
    • Answer #1 (by Timothy A. Budd)
      • Everything is an object
      • Objects communicate by sending messages
      • Objects have private state
      • Every object is an instance of one class
      • A class can inherit from another class
    What is Object-Orientation?
  12. Introduction
    • Answer #2 (by Benjamin C. Pierce)
      • Dynamic Dispatch
      • Encapsulation
      • Interface Inheritance
      • Implementation Inheritance
      • Open Recursion
    What is Object-Orientation?
  13. Introduction
    • Answer #2 (by Benjamin C. Pierce)
      • Dynamic Dispatch
        • Process of mapping a message to code at runtime
      • Encapsulation
      • Interface Inheritance
      • Implementation Inheritance
      • Open Recursion
    What is Object-Orientation?
  14. Introduction
    • Answer #2 (by Benjamin C. Pierce)
      • Dynamic Dispatch
      • Encapsulation or Information Hiding
        • Hiding of design decisions that are most likely to change
      • Interface Inheritance
      • Implementation Inheritance
      • Open Recursion
    What is Object-Orientation?
  15. Introduction
    • Answer #2 (by Benjamin C. Pierce)
      • Dynamic Dispatch
      • Encapsulation
      • Interface Inheritance
        • Classes share a set of messages they understand
      • Implementation Inheritance
      • Open Recursion
    What is Object-Orientation?
  16. Introduction
    • Answer #2 (by Benjamin C. Pierce)
      • Dynamic Dispatch
      • Encapsulation
      • Interface Inheritance
      • Implementation Inheritance
        • Classes share code
      • Open Recursion
    What is Object-Orientation?
  17. Introduction
    • Answer #2 (by Benjamin C. Pierce)
      • Dynamic Dispatch
      • Encapsulation
      • Interface Inheritance
      • Implementation Inheritance
      • Open Recursion
        • A method can invoke another method on the same object via a special, late-bound variable
    What is Object-Orientation?
  18. OOP Syntax and Semantics in PHP <?php class Tomato { } Class Declaration
  19. OOP Syntax and Semantics in PHP <?php require_once 'Tomato.php' ; $tomato = new Tomato ; Object Instantiation
  20. OOP Syntax and Semantics in PHP <?php $classes = array( 'Tomato' => 'Tomato.php' ); function __ autoload ( $className ) { if ( isset ( $classes [ $className ])) { require_once $classes [ $className ]; } } $tomato = new Tomato; Class Loading with __autoload()
  21. OOP Syntax and Semantics in PHP <?php class Tomato { protected $utilized = FALSE ; } Attributes: Object-Scope Variables
  22. OOP Syntax and Semantics in PHP <?php class Tomato { protected $utilized = FALSE; public function utilize () { $this -> utilized = TRUE ; } } Methods: Object-Scope Functions
  23. <?php class Tomato { protected $utilized = FALSE; public function utilize() { $this ->utilized = TRUE; } } Late-bound variable that references the object that the method in which it is used has been called on OOP Syntax and Semantics in PHP Methods: Object-Scope Functions
  24. OOP Syntax and Semantics in PHP <?php class Example { private $a = 'Can only be accessed by objects of the Example class' ; protected $b = 'Can only be accessed by objects of the Example class or objects of classes that extend the Example class' ; public $c = 'Can be accessed from every scope' ; } Visibility of Attributes and Methods
  25. OOP Syntax and Semantics in PHP <?php $tomato = new Tomato; $tomato -> utilize (); Method Invokation
  26. OOP Syntax and Semantics in PHP <?php class Foo { public $bar = 'baz' ; } $foo = new Foo ; print $foo -> bar ; baz Visibility of Attributes and Methods
  27. OOP Syntax and Semantics in PHP <?php class Foo { private $bar = 'baz' ; } $foo = new Foo; print $foo->bar; Fatal error: Cannot access private property Foo::$bar in /tmp/test.php on line 11 Visibility of Attributes and Methods
  28. OOP Syntax and Semantics in PHP <?php class Foo { private $bar = 'baz'; public function doSomething(Foo $foo) { print $foo->bar; } } $foo = new Foo; $foo -> doSomething (new Foo ); baz Visibility of Attributes and Methods
  29. OOP Syntax and Semantics in PHP <?php class Tomato { protected $utilized = FALSE; public function utilize () { if (! $this -> utilized) { $this -> utilized = TRUE ; } else { throw new Exception ( 'Already utilized.' ); } } } Raising Errors
  30. OOP Syntax and Semantics in PHP <?php $tomato = new Tomato; try { $tomato->utilize(); } catch ( Exception $e ) { print $e -> getMessage (); } Handling Errors
  31. OOP Syntax and Semantics in PHP <?php class Tomato { protected $utilized; public function __construct () { $this -> utilized = FALSE ; } } Special Methods: Constructor
  32. OOP Syntax and Semantics in PHP <?php class Tomato { protected $utilized; public function __construct() { $this->utilized = FALSE; } public function __destruct () { if (! $this -> utilized ) { print &quot;Tomato spoiled. &quot; ; } } } Special Methods: Destructor
  33. OOP Syntax and Semantics in PHP <?php $a = new StdClass ; // Create new object $b = $a ; // Reference object from another // another variable var_dump ( $a == $b ); // bool(true) var_dump ( $a === $b ); // bool(true) Object Equality and Object Identity
  34. OOP Syntax and Semantics in PHP <?php $a = new StdClass; // Create new object $b = $a; // Reference object from another // another variable var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(true) $a -> foo = 'bar' ; // Set an attribute of the // object through one variable var_dump ( $a -> foo ); // string(3) &quot;bar&quot; var_dump ( $b -> foo ); // string(3) &quot;bar&quot; Object Equality and Object Identity
  35. OOP Syntax and Semantics in PHP <?php $a = new StdClass; // Create new object $b = $a; // Reference object from another // another variable var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(true) $b = clone $a ; // Create a copy of the object // referenced by $a var_dump ( $a == $b ); // bool(true) var_dump ( $a === $b ); // bool(false) Object Equality and Object Identity
  36. OOP Syntax and Semantics in PHP <?php $a = new StdClass; // Create new object $b = $a; // Reference object from another // another variable var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(true) $b = clone $a; // Create a copy of the object // referenced by $a var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(false) $a -> foo = 'bar' ; var_dump ( $a == $b ); // bool(false) Object Equality and Object Identity
  37. OOP Syntax and Semantics in PHP <?php class Example { public function __ construct () { print &quot;Example:: __ construct() called. &quot; ; } public function __ clone () { print &quot;Example:: __ clone() called. &quot; ; } } $a = new Example ; $b = clone $a ; ?> Example:: __ construct() called. Example:: __ clone() called. Special Methods: Clone Constructor
  38. Circular References <?php $root = new Node ;
  39. Circular References <?php $root = new Node; $root -> addChild (new Node );
  40. Circular References <?php $root = new Node; $root->addChild(new Node); unset( $root ); PHP 5.2
    • PHP 5.3 introduces a Garbage Collector that can deal with circular references
  41. Inheritance
    • Single Inheritance
      • A class can inherit interface and implementation from a single parent class
  42. Inheritance
    • Single Inheritance
      • A class can inherit interface and implementation from a single parent class
    • Interfaces
      • A class can inherit interface and implementation from a single parent class
      • A class can implement multiple interfaces
  43. Inheritance
    • Single Inheritance
      • A class can inherit interface and implementation from a single parent class
    • Interfaces
      • A class can inherit interface and implementation from a single parent class
      • A class can implement multiple interfaces
    • Multiple Inheritance
      • A class can inherit interface and implementation from multiple parent classes
  44. Inheritance <?php class Tomato {}
  45. Inheritance <?php class Vegetable {} class Tomato extends Vegetable {}
    • Vegetable
    • Tomato
      • is a type of Vegetable
  46. Inheritance <?php abstract class Vegetable {} class Tomato extends Vegetable {}
    • Vegetable
      • is something abstract
    • Tomato
      • is a type of Vegetable
      • is something concrete
  47. Inheritance <?php abstract class Item {} abstract class Food extends Item {} abstract class Vegetable extends Food {} class Tomato extends Vegetable {}
    • Item
      • is something abstract
    • Food
      • is a type of Item
      • is something abstract
    • Vegetable
      • is a type of Food
      • is something abstract
    • Tomato
      • is a type of Vegetable
      • is something concrete
  48. Inheritance <?php interface Edible {} interface Rollable {} interface Sellable {} interface Squeezable {} abstract class Item implements Sellable {} abstract class Food extends Item implements Edible {} abstract class Vegetable extends Food {} class Tomato extends Vegetable implements Rollable , Squeezable {}
    • Item
      • is something that can be sold
      • is something abstract
    • Food
      • is a type of Item
      • Is something that can be eaten
      • is something abstract
    • Vegetable
      • is a type of Food
      • is something abstract
    • Tomato
      • is a type of Vegetable
      • is something that can be squeezed
      • is something that can be rolled
      • is something concrete
  49. Inheritance <?php interface Edible {} interface Rollable {} interface Sellable {} interface Squeezable {} abstract class Item implements Sellable {} abstract class Food extends Item implements Edible {} abstract class Fruit extends Food {} abstract class Vegetable extends Food {} class Banana extends Fruit implements Squeezable {} class Tomato extends Vegetable implements Rollable, Squeezable {}
    • Item
      • is something that can be sold
      • is something abstract
    • Food
      • is a type of Item
      • Is something that can be eaten
      • is something abstract
    • Fruit
      • is a type of Food
      • is something abstract
    • Banana
      • is a type of Fruit
      • is something that can be squeezed
      • is something concrete
  50. Inheritance <?php interface Rollable { public function roll (); } abstract class Item {} abstract class Food extends Item {} abstract class Vegetable extends Food {} class Tomato extends Vegetable implements Rollable { public function roll () { // do something } }
  51. Inheritance <?php interface Rollable { public function roll(); } interface Sqeezable { public function sqeeze (); } abstract class Item {} abstract class Food extends Item {} abstract class Vegetable extends Food {} class Tomato extends Vegetable implements Rollable, Sqeezable { public function roll() { // do something; } public function squeeze () { $this -> utilize (); return 'tomato juice' ; } }
  52. Inheritance <?php class Juicer { public function squeeze ( Squeezable $squeezable ) { $juice = $squeezable -> squeeze (); } }
  53. Inheritance <?php class Juicer { public function squeeze(Squeezable $squeezable) { $juice = $squeezable->squeeze(); } } class Roller { public function roll ( Rollable $rollable ) { $rollable -> roll (); } }
  54. Object Context <?php class Food extends Item { protected $utilized ; public function __construct () { printf( &quot;%s harvested &quot; , get_class ( $this )); $this -> utilized = FALSE ; } public function __destruct () { if (! $this -> utilized ) { printf( &quot;%s spoiled &quot; , get_class ( $this )); } } public function utilize () { $this -> utilized = TRUE ; } }
  55. Class Context <?php class Food extends Item { protected $utilized; protected static $statistics = array(); public function __construct() { printf(&quot;%s harvested &quot;, get_class($this)); $this->utilized = FALSE; self:: $statistics [ get_class ( $this )][ 'harvested' ]++; } public function __destruct() { if (!$this->utilized) { printf(&quot;%s spoiled &quot;, get_class($this)); self:: $statistics [ get_class ( $this )][ 'spoiled' ]++; } } public function utilize() { $this->utilized = TRUE; self:: $statistics [ get_class ( $this )][ 'utilized' ]++; } } Class Attributes
  56. Class Context <?php class Food extends Item { // ... public static function utilizationRate ( $food ) { return (self:: $statistics [ $food ][ 'utilized' ] / self:: $statistics [ $food ][ 'harvested' ]) * 100 . '%' ; } } Class Methods
  57. Class Context <?php class Food extends Item { // ... public static function utilizationRate($food) { return (self::$statistics[$food]['utilized'] / self::$statistics[$food]['harvested']) * 100 . '%'; } } $tomato = new Tomato ; print Food :: utilizationRate ( 'Tomato' ); Tomato harvested 0% Tomato spoiled Class Methods
  58. Class Context <?php class Food extends Item { // ... public static function utilizationRate($food) { return (self::$statistics[$food]['utilized'] / self::$statistics[$food]['harvested']) * 100 . '%'; } } $tomato = new Tomato; print Food::utilizationRate('Tomato'); $juicer = new Juicer ; $juicer -> squeeze (new Tomato ); print Food :: utilizationRate ( 'Tomato' ); Tomato harvested 0% Tomato harvested Tomato utilized 50% Tomato spoiled Class Methods
  59. Class Context <?php class Base { public static function a () { print &quot;Base::a() called &quot; ; self:: b (); } public static function b () { print &quot;Base::b() called &quot; ; } } Base :: a (); Base::a() called Base::b() called Early Static Binding
  60. Class Context <?php class Base { public static function a() { print &quot;Base::a() called &quot;; self::b(); } public static function b() { print &quot;Base::b() called &quot;; } } class Child extends Base { public static function b () { print &quot;Child::b() called &quot; ; } } Child :: a (); Base::a() called Base::b() called Early Static Binding
  61. Class Context <?php class Base { public static function a() { print &quot;Base::a() called &quot;; Base :: b (); } public static function b() { print &quot;Base::b() called &quot;; } } class Child extends Base { public static function b() { print &quot;Child::b() called &quot;; } } Child::a(); Base::a() called Base::b() called Early Static Binding
    • self is replaced with the name of the class it is used in at compile-time
  62. Class Context <?php class Base { public static function a() { print &quot;Base::a() called &quot;; static :: b (); } public static function b() { print &quot;Base::b() called &quot;; } } class Child extends Base { public static function b() { print &quot;Child::b() called &quot;; } } Child :: a (); Base::a() called Child::b() called Late Static Binding
    • self is replaced with the name of the class it is used in at compile-time
    • static is late-bound and evaluated at runtime
  63. Class Context <?php class Example { const SOME_CONSTANT = 'some value' ; public function aMethod ( $arg = self:: SOME_CONSTANT ) { } } print Example :: SOME_CONSTANT ; some value Class Constants
  64. Interceptors
    • __call($methodName, $arguments)
    • __callStatic($methodName, $arguments)
    • __get($attributeName)
    • __set($attributeName, $value)
    Interested in usage examples of __get() and __set() , as well as examples for the Reflection API, Traits, and other cool stuff? Come to my session tomorrow! Type-Safe Objects in PHP 11:15 – 12:15
  65. The End
    • Thank you for your interest!
    • These slides will be available shortly on http://sebastian-bergmann.de/talks/.
  66. License
    • This presentation material is published under the Attribution-Share Alike 3.0 Unported license.
    • You are free:
        • to Share – to copy, distribute and transmit the work.
        • to Remix – to adapt the work.
    • Under the following conditions:
        • Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
        • Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
    • For any reuse or distribution, you must make clear to others the license terms of this work.
    • Any of the above conditions can be waived if you get permission from the copyright holder.
    • Nothing in this license impairs or restricts the author's moral rights.

+ Sebastian BergmannSebastian Bergmann, 2 years ago

custom

19167 views, 108 favs, 52 embeds more stats

Initially designed for “simple” Web programming more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 19167
    • 16720 on SlideShare
    • 2447 from embeds
  • Comments 3
  • Favorites 108
  • Downloads 0
Most viewed embeds
  • 1188 views on http://woork.blogspot.com
  • 410 views on http://sebastian-bergmann.de
  • 156 views on http://www.dreamcss.com
  • 84 views on http://www.phpinternals.com
  • 83 views on http://dreamcss.blogspot.com

more

All embeds
  • 1188 views on http://woork.blogspot.com
  • 410 views on http://sebastian-bergmann.de
  • 156 views on http://www.dreamcss.com
  • 84 views on http://www.phpinternals.com
  • 83 views on http://dreamcss.blogspot.com
  • 79 views on http://cachi.temiga.org
  • 62 views on http://hpfloresj.blogspot.com
  • 57 views on http://www.getincss.ru
  • 43 views on http://zendhispano.blogspot.com
  • 41 views on http://www.phphatesme.com
  • 34 views on http://www.neatstudio.com
  • 28 views on http://www.planet-php.net
  • 28 views on http://blog.reflectiv.net
  • 20 views on http://min2liz.net
  • 18 views on http://www.blogdopedro.net
  • 15 views on http://blog.pablo-morales.com
  • 14 views on http://acidminds.com
  • 11 views on http://www.neatcn.com
  • 11 views on http://www.planet-php.org
  • 7 views on http://static.slideshare.net
  • 5 views on http://labs.friendsinteraction.com
  • 5 views on http://lj-toys.com
  • 4 views on http://www.andrezorzo.urlcurta.com
  • 4 views on http://unixbsd.blogspot.com
  • 4 views on http://blog.theemann.dk
  • 3 views on http://www.reflectiv.net
  • 3 views on http://inmyfield.nsdesign2.net
  • 3 views on http://zeta
  • 2 views on http://planet-php.org
  • 2 views on http://www
  • 2 views on http://static.slidesharecdn.com
  • 1 views on http://xianguo.com
  • 1 views on http://planet-php.net
  • 1 views on http://www.microsofttranslator.com
  • 1 views on http://in.search.yahoo.com
  • 1 views on http://reinerlee.blogspot.com
  • 1 views on http://cache.baidu.com
  • 1 views on http://www.danielwang.cn
  • 1 views on http://lamp.jiecn.net
  • 1 views on applewebdata://BEC38814-ED2A-486B-8D79-0F0ADD909A4B
  • 1 views on http://wildfire.gigya.com
  • 1 views on file://
  • 1 views on http://phporm.insite
  • 1 views on http://209.85.215.104
  • 1 views on http://feeds.feedburner.com
  • 1 views on http://ie.ort.edu.uy
  • 1 views on http://feeds2.feedburner.com
  • 1 views on http://www.hanrss.com
  • 1 views on http://www.woork.blogspot.com
  • 1 views on http://www.xianguo.com
  • 1 views on http://www.phpeye.com
  • 1 views on http://web-team.net.ua

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories

Groups / Events