Bug #66917 [Com]: __set will not called in any situation
| From: | daniel dot chobe at nextiraone dot eu | Date: | Tue, 18 Mar 2014 06:35:04 +0000 |
| Subject: | Bug #66917 [Com]: __set will not called in any situation | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-184818@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66917&edit=1
ID: 66917
Comment by: daniel dot chobe at nextiraone dot eu
Reported by: daniel dot chobe at nextiraone dot eu
Summary: __set will not called in any situation
Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Ok, I don't like this behavior, it is not logical.
But anyway is it possible to update the documentation to describe this behavior to other people?
Previous Comments:
------------------------------------------------------------------------
[2014-03-17 16:21:24] requinix@php.net
That is true: when your __set() defined $name on the instance it was no longer "not
defined" and thus the method will not be called for it in the future.
If you don't want that to happen then don't define $name on the instance - stick it in an
array or something.
------------------------------------------------------------------------
[2014-03-17 14:04:32] daniel dot chobe at nextiraone dot eu
Description:
------------
The __set Magic Method will be called only one time if the variable is not defined. If the variable
defined, __set will not called anymore, but the value is changed. See Example.
Test script:
---------------
<?php
class test {
public function __set($name, $value) {
echo "Set $name to $value" . PHP_EOL;
$this->$name = $value;
}
}
$test = new test();
$test->foo = 1;
echo 'Foo: ' . $test->foo . PHP_EOL;
$test->foo = 2;
echo 'Foo: ' . $test->foo . PHP_EOL;
Expected result:
----------------
Set foo to 1
Foo: 1
Set foo to 2
Foo: 2
Actual result:
--------------
Set foo to 1
Foo: 1
Foo: 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66917&edit=1