ID:               47567
 Updated by:       [email protected]
 Reported By:      vyk2rr at gmail dot com
-Status:           Assigned
+Status:           Feedback
 Bug Type:         Scripting Engine problem
 Operating System: Ubuntu 8.10
 PHP Version:      5.2.9
 Assigned To:      dmitry
 New Comment:

In this example the __set() handler isn't called for $t->c[] = ...;
statements. Note that __set() is called only when you assign to
overloaded property itself but not to its part. In case you are trying
to access a part of a property __get() handler is called.

Especially for this case you can use __get() handler returning by
reference. Addition of the following method makes script work in the way
you expected.

public function &__get($k) { return $this->vars[$k]; }


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

[2009-03-10 10:35:51] [email protected]

Dmitry, you closed bug #33941 with comment "this is not fixable". Can
you explain why it isn't? :)

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

[2009-03-04 23:55:47] vyk2rr at gmail dot com

Description:
------------
I can't add values to an array using __set($k, $v){...}

This look like this other bug http://bugs.php.net/bug.php?id=39449 but
is not the same

This is the same bug http://bugs.php.net/bug.php?id=33941 but is closed
:S

So I'm researching in the manual
(http://www.php.net/manual/en/language.oop5.overloading.php), but there
is nothing about it

Reproduce code:
---------------
<?php
class test {
        private $vars = array();
        public function __set($k, $v) { $this->vars[$k] = $v; }
        public function showAll(){
                echo "<pre>";
                foreach($this->vars as $k=>$v){
                        echo "$k: ";
                        print_r($v); echo "<br />";
                }
                echo "</pre>";
        }
}

$t = new test();
$t->a = 'one';
$t->b = 'two';
$t->c = array('one','two');
$t->c[] = 'three';
$t->c[] = 'four';
$t->d = array( );
$t->d[] = 'one';
$t->e = 'three';

$t->showAll();
?>

Expected result:
----------------
a: one
b: two
c: Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)

d: Array
(
    [0] => one
)

e: three

Actual result:
--------------
a: one
b: two
c: Array
(
    [0] => one
    [1] => two
)

d: Array
(
)

e: three


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


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

Reply via email to