php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33931 __get and __set don't handle arrays correctly
Submitted: 2005-07-30 22:39 UTC Modified: 2006-08-01 23:06 UTC
Votes:6
Avg. Score:4.8 ± 0.4
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:1 (16.7%)
From: wkonkel at gmail dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-08-19 OS: *
Private report: No CVE-ID: None
 [2005-07-30 22:39 UTC] wkonkel at gmail dot com
Description:
------------
The __set and __get don't seem to handle arrays correctly.  What's even worse, is it adds data to memory which is allocated at a later time ($boz gets foo2 and foo3 of $myObj).  The code example speaks for itself.  

Reproduce code:
---------------
<?

class buggy {
	private $data;
	function __set($key, $val) {
		$this->data[$key] = $val;
	}
	function __get($key) {
		return $this->data[$key];
	}
}

$myObj = new buggy();
$myObj->somevar['foo1'] = 'bar1';
$myObj->somevar['foo2'] = 'bar2';
$myObj->somevar['foo3'] = 'bar3';

$boz[] = 'blah';
print_r($boz);

?>

Expected result:
----------------
Array
(
    [0] => blah
)

Actual result:
--------------
Array
(
    [foo2] => bar2
    [foo3] => bar3
    [0] => blah
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-30 23:52 UTC] sean@php.net
Happens to me on 5.0.3 as well.

Shorter code:
<?php
class A {
  public function __set($key, $val) {
    $this->data[$key] = $val;
  }
}
$A = new A();
$A->foo['bar'] = 'oink';
$A->foo['bar1'] = 'oink1';
var_dump($undefined);
$undefined[] = 'blah';
var_dump($undefined);
?>

Outputs:
NULL
array(2) {
  ["bar1"]=>
  string(5) "oink1"
  [0]=>
  string(4) "blah"
}

 [2005-07-31 20:00 UTC] sniper@php.net
Output with CVS HEAD:

Array
(
    [foo2] => bar2
    [foo3] => bar3
    [0] => blah
)
/usr/src/php/php5/Zend/zend_hash.c(242) :  Freeing 0x088EF6FC (40 bytes), script=t.php
Last leak repeated 2 times
/usr/src/php/php5/Zend/zend_variables.h(45) :  Freeing 0x088EF664 (5 bytes), script=t.php
/usr/src/php/php5/Zend/zend_variables.c(120) : Actual location (location was relayed)
Last leak repeated 2 times
/usr/src/php/php5/Zend/zend_execute.c(798) :  Freeing 0x088EF624 (16 bytes), script=t.php
Last leak repeated 2 times
/usr/src/php/php5/Zend/zend_execute.c(1031) :  Freeing 0x088EF56C (44 bytes), script=t.php
/usr/src/php/php5/Zend/zend_API.c(712) : Actual location (location was relayed)
Last leak repeated 3 times
/usr/src/php/php5/Zend/zend_execute.c(1028) :  Freeing 0x088DE7CC (16 bytes), script=t.php
=== Total 14 memory leaks detected ===

 [2005-07-31 20:01 UTC] sniper@php.net
And this with Sean's code:

PHP Notice:  Undefined property:  A::$foo in /home/jani/t.php on line 8

Notice: Undefined property:  A::$foo in /home/jani/t.php on line 8
PHP Notice:  Undefined property:  A::$foo in /home/jani/t.php on line 9

Notice: Undefined property:  A::$foo in /home/jani/t.php on line 9
PHP Notice:  Undefined variable: undefined in /home/jani/t.php on line 10

Notice: Undefined variable: undefined in /home/jani/t.php on line 10
NULL
array(2) {
  ["bar1"]=>
  string(5) "oink1"
  [0]=>
  string(4) "blah"
}
/usr/src/php/php5/Zend/zend_variables.h(45) :  Freeing 0x088ED84C (6 bytes), script=t.php
/usr/src/php/php5/Zend/zend_variables.c(120) : Actual location (location was relayed)
Last leak repeated 1 time
/usr/src/php/php5/Zend/zend_execute.c(798) :  Freeing 0x088ED80C (16 bytes), script=t.php
Last leak repeated 1 time
/usr/src/php/php5/Zend/zend_hash.c(242) :  Freeing 0x088ED7B4 (40 bytes), script=t.php
Last leak repeated 1 time
/usr/src/php/php5/Zend/zend_execute.c(1031) :  Freeing 0x088ED754 (44 bytes), script=t.php
/usr/src/php/php5/Zend/zend_API.c(712) : Actual location (location was relayed)
Last leak repeated 3 times
/usr/src/php/php5/Zend/zend_execute.c(1028) :  Freeing 0x088DE7CC (16 bytes), script=t.php
=== Total 11 memory leaks detected ===

 [2005-08-01 09:51 UTC] tony2001@php.net
Dmitry, could you check it plz ?
 [2005-09-17 00:48 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip

I'm unable to replicate it anymore.
 [2005-09-24 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-01-10 08:46 UTC] matt at matttoddphoto dot com
I have had the same problem. This is terrible in the sense 
that I'd love to have this work, yet it doesn't. Shame.

When I have:

class Response {
	private $collection;

	public function __set($name, $value) {
		$this->collection[$name] = $value;
	}
	public function __get($name) {
		return $this->collection[$name];
	}
}

The following does not assign anything but empty array()s 
into the private $collection property.

For instance (which is called from within another class, 
hence the $this reference):

foreach($posts as $post) {
	$this->response->posts[$post['id']] = $post;
}


Now,

print_r($this->response->posts);

will output:

Array();

N.B. -- I had not noticed the bizarre activity with the 
properties reappearing in sequence immediately. In fact, I 
couldn't explain the bizarre behavior for two separate, 
nearly-identical loops like this one resulted in all of the 
data in the final of the two. Now I understand why it was 
combining the two. Bizarre indeed!
 [2006-07-26 09:39 UTC] christianl at ossafrica dot com
I have the same problem on Apache 2.0.55, PHP 5.1.4. I'm trying to access object properties with __set and __get. In my application its difficult to see whats causing it, but this seems to concur with some of the behaviour I'm getting. (Array values being set to 0). On occasion the page is output to download instead of parsed and I get a segmentation fault in Apache. The reproduce code outputs the above mentioned actual result.
 [2006-07-27 13:19 UTC] wkonkel at gmail dot com
This is still a problem.
 [2006-08-01 23:06 UTC] tony2001@php.net
Not reproducible with 5.2-CVS.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 00:01:27 2024 UTC