I guess what Mathieu is trying to say is that this:

    class Foo
    {
        public $bar = array();
    }

    $foo = new Foo;
    $foo->bar[3] = 1;

    var_dump($foo);




...is inconsistent with this:

    class Foo
    {
        public function __get($property)
        {
            if (! isset($this->$property)) {
                $this->$property = array();
            }

            return $this->$property;
        }
    }

    $foo = new Foo;
    $foo->bar[3] = 1;

    var_dump($foo);




...or even this:

    class Foo
    {
        private $bar = array();

        public function __get($property)
        {
            return $this->$property;
        }
    }

    $foo = new Foo;
    $foo->bar[3] = 1;

    var_dump($foo);


Now, I'm not really sure this is that bad, as there may be use cases where one would like to return different values every time __get is called. And, as I wrote in a previous email, there are ways to work around this inconsistency by using return by reference, so everybody can be happy. I think.



On 3/19/10 4:56 AM, Etienne Kneuss wrote:
On Thu, Mar 18, 2010 at 5:47 PM, mathieu.suen
<mathieu.s...@easyflirt.com>  wrote:
Peter Lind wrote:

On the contrary, it's quite obvious what's going on. In both examples
__get() returns an array as PHP would normally do it (i.e. NOT by
reference) which means that if you try to modify that you'll end up
modifying nothing much. However, in your second example, the point at
which you call __get() indirectly comes before the assign to the zork
array - hence, the $this->zork['blah'] = 'blah'; no longer indirectly
calls __get as object::$zork now exists.

  In other words, this is down to you confusing passing a variable by
reference and passing it by value: PHP normally passes arrays by
value, so when __get() returns an array, you're working on a copy of
the array you returned. As someone noted earlier, you can easily
change the behaviour of __get to return variables by reference, should
you want to. However, I personally wouldn't want this to be default
behaviour as that would make debugging apps much more annoying -
things should be consistent, even if consistency at times confuse
people.

Regards
Peter



The sementic of

$this->zork

Should be the same as

$this->__get('zork')


$this->zork is only the same as $this->__get("zork") if zork is
undefined, this is perfectly normal and expected.

So in that respect it is not consistent.

But anywhere I don't care if  it change or not.

Look at Scheme, Lisp and Smalltalk language.
This is what I call consistent language.


-- Mathieu Suen





--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php






--
Ionut G. Stan
I'm under construction  |  http://blog.igstan.ro/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to