ID:               39337
 User updated by:  phpbugs at thequod dot de
 Reported By:      phpbugs at thequod dot de
-Status:           Bogus
+Status:           Open
 Bug Type:         Arrays related
 Operating System: Ubuntu Linux
 PHP Version:      5CVS-2006-11-01 (CVS)
 New Comment:

Ok. I've even slept over it.

Why should this not work?

---------------------------------
<?php
class A
{
        function __get($v)
        {
                if( isset($this->var) )
                        return $this->var;
                return array();
        }
}


$A = new A();

$A->foo[] = 1;
var_dump( $A->foo );
?>
---------------------------------

It prints:
array(0) {
}

Your comment, which I've reread carefully, does not 
explain it. The temp var from __get() is first array and 
later the $var itself.

Adding a __set() method to the class shows that this does 
not get called at all.


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

[2006-11-09 00:30:50] [EMAIL PROTECTED]

I've already explained everything, just read my previous comment
carefully.

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

[2006-11-09 00:22:16] phpbugs at thequod dot de

Sorry, to reopen it again.

The "Reproduce code" in the comment above was wrong.

I've let it return "array()" always and this changes the 
output, so that it is "array()" instead of "NULL".

But still: the array does not get changed!

I've written a comment for Bug 39426, which seems to 
suffer from something related, but cannot post it, because 
it's bogus.. :/

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

[2006-11-08 23:47:42] [EMAIL PROTECTED]

Yes, I did see the comment. 
The point is that with $A->foo[] = 1; you call __get() first, which
returns NULL and you modify this temporary variable. That happens
because we need to get the array itself to be able to add an element,
but to create a variable directly (like $A->foo = 1) this is not
needed.


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

[2006-11-08 23:18:36] phpbugs at thequod dot de

Ok. But haven't you seen the comment in __get()?

Here's another testcase, just returning an array now 
always and with further output, when __get() gets called:


Description:
------------
When using "array creating syntax" (like $a[] or $a[1]), 
__get() does not seem to work correctly, IF the var has 
not been defined using the "var" key for the class.

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

class A
{
        function __get($v)
        {
                // note: even returning array() here won't 
fix it  
        }
}


$A = new A();

$A->foo[1] = 1;
var_dump( $A->foo );

$A->foo[] = 2;
var_dump( $A->foo );

$A->foo['a'] = 3;
var_dump( $A->foo );

$A->foo = array();
var_dump( $A->foo );

$A->foo = 1;
var_dump( $A->foo );

?>

Expected result:
----------------
__get: foo
array(1) {
  1 => 1
}
array(2) {
  1 => 1,
  2 => 2
}
array(0) {
  1 => 1,
  2 => 2,
  'a' => 3
}
array(0) {
}
int(1)


Actual result:
--------------
__get: foo
__get: foo
array(0) {
}
__get: foo
__get: foo
array(0) {
}
__get: foo
__get: foo
array(0) {
}
array(0) {
}
int(1)

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

[2006-11-08 17:40:51] [EMAIL PROTECTED]

It's not in the manual, because it's obvious - your __get() method does
nothing, so you get nothing as the result.

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

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/39337

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

Reply via email to