WRONG !
Read the manual about __set() and __get().
And read http://www.php.net/~helly/php/ext/spl/index.html about arrayAccess.
Php 5 allow you to "overloading" property dynamicaly with __set() and __get(). And you can access an object like an array with arrayAccess interface.
There is no nonsense in my code.
Fred.
Daniel Kullik wrote:
Hello Frédéric.
This is neither a bug nor a paradox. The code you've posted just contains some nonsense.
$foo['bar'] = 'bar'; cannot work since $foo is an object and not an array. And even $foo->bar = 'bar'; cannot work because there is no property $bar.
Frédéric hardy wrote:
Hello -
I think there is a bug or a paradox in the php 5 object model implementation.
This is an example :
<?php
class foo implements arrayAccess { private $array = array();
function __construct() {}
function __get($key) { return $this->offsetGet($key); }
function __set($key, $value) { $this->offsetSet($key, $value); }
function offsetExists($key) { return isset($this->array[$key]); }
function offsetGet($key) { return $this->array[$key]; }
function offsetSet($key, $value) { $this->array[$key] = $value; }
function offsetUnset($key) { unset($this->array[$key]; } }
$foo = new foo();
echo (isset($foo['bar']) == true ? 'set' : 'not set'); $foo['bar'] = 'bar'; echo (isset($foo['bar']) == true ? 'set' : 'not set'); echo $foo['bar'];
#Expected result : # not set # set # bar #Real result # not set # set # bar # !! GREAT !!
#Now, the same thing with __get() and __set()
unset($foo); $foo = new foo();
echo (isset($foo->array) == true ? 'array is set' : 'array is not set'); echo (isset($foo->bar) == true ? 'bar is set' : 'bar is not set'); $foo->bar = 'bar'; echo (isset($foo['bar']) == true ? 'bar is set' : 'bar is not set'); echo $foo->bar;
#Expected result : # array is set # bar is not set # bar is set # bar #Real result # array is set # Ok ! # bar is not set # Ok ! # bar is not set # PROBLEM PROBLEM # bar # !! NOT GREAT !!
?>
It is very strange.
isset() does not return the good value on property wich was set with __set() !!
But isset() return the good value on property wich was set in the class !!
And isset() return the good value on value wich was set with offsetSet() method !!
It is a paradox !
I think that isset MUST return the same value in all case.
What do you think of ?
Fred.
Warning : the php code may be wrong (parse error...), i can not validate it in php5 currently
-- =================================================================== Frederic HARDY Email: [EMAIL PROTECTED] HEXANET SARL URL: http://www.hexanet.fr/ ZAC Les Charmilles Tel: +33 (0)3 26 79 30 05 3, allée Thierry Sabine Direct: +33 (0)3 26 61 77 84 BP 202 - 51686 REIMS CEDEX 2 FRANCE ===================================================================
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php