Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TEST--
- Returning an overloaded property by reference
- --FILE--
- <?php
- class C {
- function &__get($unused) {
- global $global;
- return $global;
- }
- }
- function &getByRefExplicit() {
- $c = new C;
- return $c->__get('a');
- }
- function &getByRefImplicit() {
- $c = new C;
- // The problem only occurs if the property is returned directly.
- // Uncommenting the following 2 lines works around the problem:
- // $r =& $c->a;
- // return $r;
- return $c->a;
- }
- $global = 'original';
- echo "Get reference to global via return-by-ref __get() called implicitly - OK:\n";
- $c = new C;
- $ref1 =& $c->a;
- $ref1 = 'First change!';
- var_dump($global);
- unset($global);
- $global = 'original';
- echo "\nGet reference to global via return-by-ref __get() called explicitly from return-by-ref function - OK:\n";
- $ref2 =& getByRefExplicit();
- $ref2 = 'Second change!';
- var_dump($global);
- unset($global);
- $global = 'original';
- echo "\nGet reference to global via return-by-ref __get() called implicitly from return-by-ref function:\n";
- $ref3 =& getByRefImplicit();
- $ref3 = 'Third change!';
- var_dump($global);
- ?>
- --EXPECTF--
- Get reference to global via return-by-ref __get() called implicitly - OK:
- string(13) "First change!"
- Get reference to global via return-by-ref __get() called explicitly from return-by-ref function - OK:
- string(14) "Second change!"
- Get reference to global via return-by-ref __get() called implicitly from return-by-ref function:
- string(13) "Third change!"
Advertisement
Add Comment
Please, Sign In to add comment