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.

0 comments

Post a comment

    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.

    9 Favorites

    Understanding the PHP Object Model - Presentation Transcript

    1. Welcome! Understanding the PHP Object Model Sebastian Bergmann http://sebastian-bergmann.de/ May 28 th 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)
      • __isset($attributeName)
    65. Storing Data in Objects <?php class Employee { public $id ; public $name ; }
      • Attributes publicly accessible
        • Attributes are also called fields or slots sometimes
      • No enforcement of value types
      Public Attributes
    66. Storing Data in Objects <?php class Employee { protected $id ; protected $name ; public function getId () { return $this -> id ; } public function setId ( $id ) { if ( is_int ( $id )) { $this -> id = $id ; } else { throw new InvalidArgumentException ; } } // ... }
      • Attributes not directly accessible
      • Enforcement of value types in setter methods
      Protected Attributes and Accessor Methods
    67. Storing Data in Objects <?php class Employee { protected $id ; protected $name ; public function getId () { return $this -> id ; } public function setId ( $id ) { if ( is_int ( $id )) { $this -> id = $id ; } else { // ... } } // ... }
      • Attributes not directly accessible
      • Enforcement of value types in setter methods
        • But attributes might still be (unintentionally) accessed directly from “within”
      Protected Attributes and Accessor Methods
    68. Storing Data in Objects <?php class Employee { protected $id ; protected $name ; public function getId () { return $this -> id ; } public function setId ( $id ) { if ( is_int ( $id )) { $this -> id = $id ; } else { // ... } } // ... }
      • Attributes not directly accessible
      • Enforcement of value types in setter methods
        • But attributes might still be (unintentionally) accessed directly from “within”
      • No syntactic relationship between the attribute and the methods that manage it
      Protected Attributes and Accessor Methods
    69. Storing Data in Objects <?php class Employee { protected $id ; protected $name ; public function getId () { return $this -> id ; } public function setId ( $id ) { if ( is_int ( $id )) { $this -> id = $id ; } else { // ... } } // ... }
      • Attributes not directly accessible
      • Enforcement of value types in setter methods
        • But attributes might still be (unintentionally) accessed directly from “within”
      • No syntactic relationship between the attribute and the methods that manage it
      • These methods have to be called explicitly by the accessing context
      Protected Attributes and Accessor Methods
    70. Storing Data in Objects Properties
      • Construct in languages such as C# or Delphi
        • Just like an attribute, but access is automatically translated into method calls
        • Use Cases
          • Type Checking
          • Bounds Checking
          • Read-Only Attributes
          • Persistence
          • ...
    71. Storing Data in Objects Properties
      • Construct in languages such as C# or Delphi
        • Just like an attribute, but access is automatically translated into method calls
        • Use Cases
          • Type Checking
          • Bounds Checking
          • Read-Only Attributes
          • Persistence
          • ...
      • Can be implemented in PHP using the __get() , __set() , and __isset() interceptors
        • This is an idiom used in the eZ Components
    72. Interlude eZ Components
      • Provide a solid platform for PHP application development
      • Clean and simple API
      • Excellent documentation
      • Keep backward compatibility for longer periods of time
      • Stable and few regressions
      • Clean IP, Open Source friendly
      • http://ezcomponents.org/
    73. Properties The eZ Components Way <?php class Employee { protected $properties = array( 'id' => NULL , 'name' => NULL ); public function __get ( $name ) { switch ( $name ) { case 'id' : case 'name' : { return $this -> properties [ $name ]; } } throw new ezcBasePropertyNotFoundException ( $name ); } // ... }
    74. Properties The eZ Components Way <?php class Employee { public function __set ( $name , $value ) { switch ( $name ) { case 'id' : { if ( is_int ( $value )) { $this -> properties [ $name ] = $value ; } } break; // ... } throw new ezcBasePropertyNotFoundException ( $name ); } // ... }
    75. Properties The eZ Components Way <?php class Employee { // ... public function __isset ( $name ) { switch ( $name ) { case 'id' : case 'name' : { return TRUE ; } } return FALSE ; } }
    76. Type-Safe Properties <?php class Employee { /** * @var integer */ protected $id ; /** * @var string */ protected $name ; }
      • By now we are used to annotate attribute declarations with type information
        • This metadata is used, for instance, by PHPDocumentor to generate API documentation
      Annotations
    77. Type-Safe Properties <?php class Employee { /** * @var integer */ protected $id ; /** * @var string */ protected $name ; }
      • By now we are used to annotate attribute declarations with type information
        • This metadata is used, for instance, by PHPDocumentor to generate API documentation
        • But it is also usefull when we want to automatically generate SQL queries or WSDL from an object
      Annotations
    78. Type-Safe Properties <?php class Employee { /** * @var integer */ protected $id ; /** * @var string */ protected $name ; }
      • By now we are used to annotate attribute declarations with type information
        • This metadata is used, for instance, by PHPDocumentor to generate API documentation
        • But it is also usefull when we want to automatically generate SQL queries or WSDL from an object
      • PHP allows read access to these docblocks at runtime through its Reflection API
        • So let us use this metadata for reusable __get() and __set() implementations
      Annotations
    79. Interlude <?php class Employee { /** * @var integer */ protected $id; /** * @var string */ protected $name; } $class = new ReflectionClass ( 'Employee' ); $attributes = $class -> getProperties (); foreach ( $attributes as $attribute ) { $docComment = $attribute -> getDocComment (); if ( preg_match ( '/@var[s]+([.w]+)/' , $docComment , $matches )) { var_dump ( $matches [ 1 ]); } } Annotations and the Reflection API
    80. Type-Safe Properties <?php abstract class TypeSafeObject { protected $annotationsParsed = FALSE ; protected $properties = array(); protected function parseAnnotations () { if (! $this -> annotationsParsed ) { $class = new ReflectionClass ( $this ); $attributes = $class -> getProperties (); foreach ( $attributes as $attribute ) { $docComment = $attribute -> getDocComment (); if ( preg_match ( '/@var[s]+([.w]+)/' , $docComment , $matches )) { $this -> properties [ $attribute -> getName ()] = array( 'type' => $matches [ 1 ], 'value' => NULL ); } } $this -> annotationsParsed = TRUE ; } } // ... The TypeSafeObject Class
    81. Type-Safe Properties public function __get ( $name ) { $this -> parseAnnotations (); if ( isset ( $this -> properties [ $name ])) { return $this-> properties [ $name ][ 'value' ]; } else { // ... } } The TypeSafeObject Class
    82. Type-Safe Properties public function __get($name) { $this->parseAnnotations(); if (isset($this->properties[$name])) { return $this->properties[$name]['value']; } else { // ... } } public function __set ( $name , $value ) { $this -> parseAnnotations (); if ( isset ( $this -> properties [ $name ]) ) { if ( gettype ( $value ) == $this -> properties [ $name ][ 'type' ]) { $this -> properties [ $name ][ 'value' ] = $value ; } else { // ... } } else { // ... } } // ... The TypeSafeObject Class
    83. Type-Safe Properties public function __isset ( $name ) { $this -> parseAnnotations (); return isset ( $this -> properties [ $name ]); } } The TypeSafeObject Class
    84. Type-Safe Properties <?php class Employee extends TypeSafeObject { /** * @var integer */ protected $id ; /** * @var string */ protected $name ; } Using the TypeSafeObject Class
    85. Type-Safe Properties <?php class Employee extends TypeSafeObject { /** * @var integer */ protected $id; /** * @var string */ protected $name; } $e = new Employee ; var_dump ( $e -> id ); NULL Using the TypeSafeObject Class
    86. Type-Safe Properties <?php class Employee extends TypeSafeObject { /** * @var integer */ protected $id; /** * @var string */ protected $name; } $e = new Employee; $e -> id = '123' ; var_dump ( $e -> id ); NULL Using the TypeSafeObject Class
    87. Type-Safe Properties <?php class Employee extends TypeSafeObject { /** * @var integer */ protected $id; /** * @var string */ protected $name; } $e = new Employee; $e -> id = 123 ; var_dump ( $e -> id ); int(123) Using the TypeSafeObject Class
    88. Type-Safe Properties <?php abstract class TypeSafeObject { // ... } class Employee extends TypeSafeObject { /** * @var integer */ protected $id ; /** * @var string */ protected $name ; } Problems with the TypeSafeObject Class
      • Conceptual problem
        • Employee is a TypeSafeObject
    89. Type-Safe Properties <?php abstract class TypeSafeObject { // ... } class Person extends TypeSafeObject { // ... } class Employee extends Person { /** * @var integer */ protected $id; /** * @var string */ protected $name; } Problems with the TypeSafeObject Class
      • Conceptual problem
        • Employee is a TypeSafeObject
        • This usually does not fit into our model
    90. Type-Safe Properties <?php abstract class TypeSafeObject { // ... } class Person extends TypeSafeObject { // ... } class Employee extends Person { /** * @var integer */ protected $id; /** * @var string */ protected $name; } Problems with the TypeSafeObject Class
      • Conceptual problem
        • Employee is a TypeSafeObject
        • This usually does not fit into our model
      • PHP 5.3 supports
        • Single Implementation Inheritance
        • Multiple Interface Inheritance
    91. Type-Safe Properties <?php trait TypeSafeObject { // ... } class Person { use TypeSafeObject ; // ... } class Employee extends Person { /** * @var integer */ protected $id; /** * @var string */ protected $name; } Traits to the rescue!
      • Conceptual problem
        • Employee is a TypeSafeObject
        • This usually does not fit into our model
      • PHP 5.3 supports
        • Single Implementation Inheritance
        • Multiple Interface Inheritance
      • Future versions of PHP will support Traits
        • Safe variant of multiple implementation inheritance
          • A class can implement multiple interfaces
          • A class can extend a single class
          • A class can use multiple traits
        • Reuse sets of methods freely in several independent classes living in different class hierarchies
    92. The End
      • Thank you for your interest!
      • These slides will be available shortly on http://sebastian-bergmann.de/talks/.
    93. 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

    4230 views, 9 favs, 8 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 4230
      • 3811 on SlideShare
      • 419 from embeds
    • Comments 0
    • Favorites 9
    • Downloads 0
    Most viewed embeds
    • 359 views on http://sebastian-bergmann.de
    • 35 views on http://www.planet-php.net
    • 10 views on http://www.planet-php.org
    • 9 views on http://www.phpreads.com
    • 3 views on http://planet-php.org

    more

    All embeds
    • 359 views on http://sebastian-bergmann.de
    • 35 views on http://www.planet-php.net
    • 10 views on http://www.planet-php.org
    • 9 views on http://www.phpreads.com
    • 3 views on http://planet-php.org
    • 1 views on http://lj-toys.com
    • 1 views on http://www.xianguo.com
    • 1 views on http://64.233.179.104

    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