php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38102 Overloading __get()/__set() should explicitly state it is return-by-value
Submitted: 2006-07-14 09:10 UTC Modified: 2007-11-02 11:16 UTC
Votes:6
Avg. Score:5.0 ± 0.0
Reproduced:6 of 6 (100.0%)
Same Version:5 (83.3%)
Same OS:3 (50.0%)
From: frode at coretrek dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2006-07-14 (snap) OS: Linux
Private report: No CVE-ID: None
 [2006-07-14 09:10 UTC] frode at coretrek dot com
Description:
------------
Assigning an array to an object field and updating it through use of __get/__set() no longer works in PHP 5.2 snapshots. This is probably related to the fix for bug 37667.

Below is a test case based on the one in bug 37667, which is similar to a larger real code base we have working in PHP 5.1.4.

This test case does not trigger bug 37667 because we create the array through __set() - bug 37667 does not set the new array this way.

"Expected result" shows output in PHP 5.1.4
"Actual result" shows output in PHP5.2-200607140630

Running on apache 2.2.2.

Reproduce code:
---------------
<?
header("Content-type: text/plain");

class Class1 {
  protected $property = array();
  function __get($name) {
    return $this->property;
  }
  function __set($name, $val) {
    $this->$name = $val;
  }
}
class Class2 {}
$r = new Class1();
$r->property = array();
$r->property["what"] = new Class2();
$r->property["who"] = new Class2();
var_dump($r);

?>


Expected result:
----------------
object(Class1)#1 (1) {
  ["property:protected"]=>
  array(2) {
    ["what"]=>
    object(Class2)#2 (0) {
    }
    ["who"]=>
    object(Class2)#3 (0) {
    }
  }
}


Actual result:
--------------
<br />
<b>Fatal error</b>:  Cannot use array returned from Class1::__get('property') in write context in <b>/devel2/frode/www/_/test37667.php</b> on line <b>16</b><br />


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-31 15:18 UTC] felipe@php.net
In PHP 5.3.0-dev (cli) (built: Oct 30 2007 08:45:02):

object(Class1)#1 (1) {
  ["property:protected"]=>
  array(0) {
  }
}

Reclassified.
 [2007-11-02 11:16 UTC] jani@php.net
Expected:

[jani@localhost ~]$ src/build/php_5_3/sapi/cli/php t.php

Notice: Indirect modification of overloaded property Class1::$property has no effect in /home/jani/t.php on line 15

Notice: Indirect modification of overloaded property Class1::$property has no effect in /home/jani/t.php on line 16
object(Class1)#1 (1) {
  ["property":protected]=>
  array(0) {
  }
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 13:01:29 2024 UTC