SHARE
    TWEET
    Guest User

    Untitled

    a guest
    Sep 13th, 2014
    2,542
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    PHP 4.54 KB | None | 0 0
    1. <?php
    2. class A
    3. {
    4. public function printVars()
    5. {
    6. $r = ' // get_called_class() == '.get_called_class().
    7. ' // __CLASS__ == '.__CLASS__.
    8. ' // get_object_vars($this):';
    9. foreach(get_object_vars($this) as $key => $val){
    10. $r .= ' ->'.$key.' == "'.$val.'"';
    11. }
    12. return $r;
    13. }
    14. }
    15. class B extends A
    16. {
    17. public function printVars() // actually overloading afaik
    18. {
    19. $r = ' // get_called_class() == '.get_called_class().
    20. ' // __CLASS__ == '.__CLASS__.
    21. ' // get_object_vars($this):';
    22. foreach(get_object_vars($this) as $key => $val){
    23. $r .= ' ->'.$key.' == "'.$val.'"';
    24. }
    25. return $r;
    26. }
    27. }
    28. class C extends B
    29. {
    30. }
    31. echo '<pre>';
    32. $a = new A;
    33. $a->that = 'that for A';
    34. $b = new B;
    35. $b->that = 'that for B';
    36. $c = new C;
    37. echo '
    38. $a = new A;
    39. $a->that = \'that for A\';
    40. $b = new B;
    41. $b->that = \'that for B\';
    42. $c = new C;
    43. $a->printVars();'.$a->printVars().'
    44. $b->printVars();'.$b->printVars().'
    45. $c->printVars();'.$c->printVars().'
    46. ';
    47. $c->that = 'that for C';
    48. echo '
    49. $c->that = \'that for C\';
    50. $a->printVars();'.$a->printVars().'
    51. $b->printVars();'.$b->printVars().'
    52. $c->printVars();'.$c->printVars().'
    53. ';
    54. echo '<hr>
    55. using __set()';
    56. class A2
    57. {
    58. // private $that;
    59. // protected $that;
    60. public function __set($name,$value)
    61. {
    62. $this->$name = $value;
    63. }
    64. public function printVars()
    65. {
    66. $r = ' // get_called_class() == '.get_called_class().
    67. ' // __CLASS__ == '.__CLASS__.
    68. ' // get_object_vars($this):';
    69. foreach(get_object_vars($this) as $key => $val){
    70. $r .= ' ->'.$key.' == "'.$val.'"';
    71. }
    72. return $r;
    73. }
    74. }
    75. class B2 extends A2
    76. {
    77. // private $that;
    78. // protected $that;
    79. public function __set($name,$value)
    80. {
    81. $this->$name = $value;
    82. }
    83. public function printVars() // actually overloading afaik
    84. {
    85. $r = ' // get_called_class() == '.get_called_class().
    86. ' // __CLASS__ == '.__CLASS__.
    87. ' // get_object_vars($this):';
    88. foreach(get_object_vars($this) as $key => $val){
    89. $r .= ' ->'.$key.' == "'.$val.'"';
    90. }
    91. return $r;
    92. }
    93. }
    94. class C2 extends B2
    95. {
    96. // private $that;
    97. // protected $that;
    98. public function __set($name,$value)
    99. {
    100. $this->$name = $value;
    101. }
    102. }
    103. echo '<pre>';
    104. $a = new A2;
    105. $a->that = 'that for A2';
    106. $b = new B2;
    107. $b->that = 'that for B2';
    108. $c = new C2;
    109. echo '
    110. $a = new A2;
    111. $a->that = \'that for A2\';
    112. $b = new B2;
    113. $b->that = \'that for B2\';
    114. $c = new C2;
    115. $a->printVars();'.$a->printVars().'
    116. $b->printVars();'.$b->printVars().'
    117. $c->printVars();'.$c->printVars().'
    118. ';
    119. $c->that = 'that for C2';
    120. echo '
    121. $c->that = \'that for C2\';
    122. $a->printVars();'.$a->printVars().'
    123. $b->printVars();'.$b->printVars().'
    124. $c->printVars();'.$c->printVars().'
    125. ';
    126. ?>
    127. output:
    128. $a = new A;
    129. $a->that = 'that for A';
    130. $b = new B;
    131. $b->that = 'that for B';
    132. $c = new C;
    133. $a->printVars(); // get_called_class() == A // __CLASS__ == A // get_object_vars($this): ->that == "that for A"
    134. $b->printVars(); // get_called_class() == B // __CLASS__ == B // get_object_vars($this): ->that == "that for B"
    135. $c->printVars(); // get_called_class() == C // __CLASS__ == B // get_object_vars($this):
    136. $c->that = 'that for C';
    137. $a->printVars(); // get_called_class() == A // __CLASS__ == A // get_object_vars($this): ->that == "that for A"
    138. $b->printVars(); // get_called_class() == B // __CLASS__ == B // get_object_vars($this): ->that == "that for B"
    139. $c->printVars(); // get_called_class() == C // __CLASS__ == B // get_object_vars($this): ->that == "that for C"
    140. using __set()
    141. $a = new A2;
    142. $a->that = 'that for A2';
    143. $b = new B2;
    144. $b->that = 'that for B2';
    145. $c = new C2;
    146. $a->printVars(); // get_called_class() == A2 // __CLASS__ == A2 // get_object_vars($this): ->that == "that for A2"
    147. $b->printVars(); // get_called_class() == B2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for B2"
    148. $c->printVars(); // get_called_class() == C2 // __CLASS__ == B2 // get_object_vars($this):
    149. $c->that = 'that for C2';
    150. $a->printVars(); // get_called_class() == A2 // __CLASS__ == A2 // get_object_vars($this): ->that == "that for A2"
    151. $b->printVars(); // get_called_class() == B2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for B2"
    152. $c->printVars(); // get_called_class() == C2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for C2"
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /