|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-08-01 05:48 UTC] gordon at heydon dot com dot au
Description:
------------
If using the __set() and __get() to store and manipulate arrays within a property, if the array is not being stored as an array then the array assignment will not work.
If in the example the property just has the value from the assignment stored without the serialization, so storing the array within the array this works.
Also the funny thing is that if you put echos within the __set() and __get the $obj->prop[5] = 'apple' does not output anything at all. It is like PHP is trying to guess what the __set() is doing and then doing that,
Reproduce code:
---------------
<?php
class ex {
public function __get($prop) {
return unserialize($this->fields[$prop]);
}
public function __set($prop, $value) {
$this->fields[$prop] = serialize($value);
}
private $fields;
}
$obj = new ex;
$obj->prop = array(4 => 'pear');
$obj->prop[5] = 'apple';
var_dump($obj->prop);
?>
Expected result:
----------------
array(1) {
[4]=>
string(4) "pear"
[5]=>
string(5) "apple"
}
Actual result:
--------------
array(1) {
[4]=>
string(4) "pear"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 15:00:01 2025 UTC |
Can we atleast make sure it gives a nice big warning on this? Now it merely produces some leaks: <?php class Foo { function __set($name, $value) { var_dump($name, $value); } } $f = new Foo(); $f->bar[1] = 42; produces: derick@kossu:~$ php-6.0dev /tmp/foooo.php /dat/dev/php/php-6.0dev/Zend/zend_execute.c(827) : Freeing 0x08796494 (16 bytes), script=/tmp/foooo.php /dat/dev/php/php-6.0dev/Zend/zend_execute.c(1005) : Freeing 0x0879643C (35 bytes), script=/tmp/foooo.php /dat/dev/php/php-6.0dev/Zend/zend_hash.c(383) : Actual location (location was relayed) /dat/dev/php/php-6.0dev/Zend/zend_execute.c(1060) : Freeing 0x087963DC (44 bytes), script=/tmp/foooo.php /dat/dev/php/php-6.0dev/Zend/zend_API.c(712) : Actual location (location was relayed) Last leak repeated 1 time /dat/dev/php/php-6.0dev/Zend/zend_execute.c(1057) : Freeing 0x087842CC (16 bytes), script=/tmp/foooo.php === Total 5 memory leaks detected ===