Guest User

robin

a guest
Jan 3rd, 2008
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. --TEST--
  2. Returning an overloaded property by reference
  3. --FILE--
  4. <?php
  5. class C {
  6.     function &__get($unused) {
  7.         global $global;
  8.         return $global;
  9.     }
  10. }
  11.  
  12. function &getByRefExplicit() {
  13.   $c = new C;
  14.   return $c->__get('a');
  15. }
  16.  
  17. function &getByRefImplicit() {
  18.   $c = new C;
  19.   // The problem only occurs if the property is returned directly.
  20.   // Uncommenting the following 2 lines works around the problem:
  21.   // $r =& $c->a;
  22.   // return $r;
  23.   return $c->a;
  24. }
  25.  
  26. $global = 'original';
  27. echo "Get reference to global via return-by-ref __get() called implicitly - OK:\n";
  28. $c = new C;
  29. $ref1 =& $c->a;
  30. $ref1 = 'First change!';
  31. var_dump($global);
  32.  
  33. unset($global);
  34. $global = 'original';
  35. echo "\nGet reference to global via return-by-ref __get() called explicitly from return-by-ref function - OK:\n";
  36. $ref2 =& getByRefExplicit();
  37. $ref2 = 'Second change!';
  38. var_dump($global);
  39.  
  40. unset($global);
  41. $global = 'original';
  42. echo "\nGet reference to global via return-by-ref __get() called implicitly from return-by-ref function:\n";
  43. $ref3 =& getByRefImplicit();
  44. $ref3 = 'Third change!';
  45. var_dump($global);
  46. ?>
  47. --EXPECTF--
  48. Get reference to global via return-by-ref __get() called implicitly - OK:
  49. string(13) "First change!"
  50.  
  51. Get reference to global via return-by-ref __get() called explicitly from return-by-ref function - OK:
  52. string(14) "Second change!"
  53.  
  54. Get reference to global via return-by-ref __get() called implicitly from return-by-ref function:
  55. string(13) "Third change!"
Advertisement
Add Comment
Please, Sign In to add comment