summaryrefslogtreecommitdiff
path: root/Zend/RFCs/004.txt
blob: 1e120ce306d52658d671ef5e719daed1858a8825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Title:           Delegation
                 (a/k/a. Object-Based Inheritance)
Version:         $Revision$
Status:          draft
Maintainer:      Sebastian Bergmann <sb@sebastian-bergmann.de>


Dynamic Delegation

  Syntax / Example

    <?php
    class aDelegatee {
      function doSomething() {
        echo 'hubu';
      }
    }

    class anotherDelegatee {
      function doSomething() {
        echo 'tubu';
      }
    }

    class Foo {
      delegatee $bar;

      function setDelegatee($delegatee) {
        $this->delegatee = $delegatee;
      }
    }

    $foo = new Foo;

    $foo->setDelegatee(new aDelegatee);
    $foo->doSomething(); /* prints "hubu" */

    $foo->setDelegatee(new anotherDelegatee);
    $foo->doSomething(); /* prints "tubu" */
    ?>

  Semantics / Synopsis

    The "Foo" class may use all methods available in "$bar" as if they 
    where locally defined or inherited from a superclass. The essential 
    difference is that by assigning another object to "$bar" it is 
    possible to dynamically switch between different implementations for 
    these methods.


Fixed Delegation

  Syntax / Example

    <?php
    class aDelegatee {
      function doSomething() {
        echo 'hubu';
      }
    }

    class Foo {
      final delegatee $bar = new aDelegatee;
    }

    $foo = new Foo;

    $foo->doSomething(); /* prints "hubu" */
    ?>

  Semantics / Synopsis

    The "Foo" class may use all methods available in "$bar" as if they 
    where locally defined or inherited from a superclass. The difference
    to the dynamic delegation is that once the delegatee object is set,
    it cannot be changed.


Default Delegation

  Syntax / Example

    <?php
      class Foo {
        function __delegate($name, $parameters = array()) {
          echo $name;
        }
      }

      $foo = new Foo;
      $foo->bar(); /* prints "bar" */
    ?>

  Semantics / Synopsis

    If a class has a __delegate() method, it is called whenever a
    method on an object of this class is called that is

      * Not defined in the class.

      * Not provided by a delegatee object.

    The __delegate() method is called with the name and parameters
    of the method call.

    This supersedes / obsoletes similar functionality introduced in
    Zend Engine 1 by ext/overload.