ID:               41387
 User updated by:  peter at ibuildings dot nl
 Reported By:      peter at ibuildings dot nl
 Status:           Open
 Bug Type:         Class/Object related
 Operating System: Windows XP Professional
 PHP Version:      5.2.2
 New Comment:

Ok, but this did work using PHP 5.1.6 so you can call this a
regression. If I understand this correctly the only way to work-around
this is using some code like the following:

$b = array();
$b[] = '1';
$b[] = '2';
$b[] = '3';
$obj->b = $b; 

Or in a more realistic case where other methods assign something to the
object:

$b = $obj->b;
$b[] = '3';
$obj->b = $b;

Seems not very intuitive to me.


Previous Comments:
------------------------------------------------------------------------

[2007-05-14 08:27:42] judas dot iscariote at gmail dot com

turn on error reporting and you will find the cause.

PHP Notice:  Indirect modification of overloaded property Data::$b has
no effect.

------------------------------------------------------------------------

[2007-05-14 08:21:25] peter at ibuildings dot nl

Description:
------------
I've created a small class that implements the magic __set, __get and
__isset methods. Using PHP 5.1.6 I can assign an array to a "fake"
instance variable and then add elements to it. Doing the same using PHP
5.2.1 or 5.2.2 doesn't change the contents of the array. 

I've tried returning a reference from the __get method (e.g. changes
the function definition to "function &__get($key, $value) ...", but this
doesn't work either (although PHP doesn't complain about it).

If this wasn't supposed to work with PHP 5.1.6 how should I then
implement this to get the desired behaviour? If this isn't possible at
all then that means __set/__get are far less usable then before.

Reproduce code:
---------------
class Data
{
  private $m_data = array();

  function __set($key, $value) { $this->m_data[$key] = $value; }
  function __get($key) { return $this->m_data[$key]; }
  function __isset($key) { return isset($this->m_data[$key]); }

  function dump() { var_dump($this->m_data); }
}

$obj = new Data();
$obj->a = "a";
$obj->b = array();
$obj->b[] = '1';
$obj->b[] = '2';
$obj->b[] = '3';
$obj->dump();

Expected result:
----------------
array(2) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
  }
}

Actual result:
--------------
array(2) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  array(0) {
  }
}


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=41387&edit=1

Reply via email to