Edit report at https://bugs.php.net/bug.php?id=71203&edit=1
ID: 71203
Updated by: [email protected]
Reported by: nowm at yandex dot ru
Summary: Ignoring class setter
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
That code triggers a getter, not a setter.
https://3v4l.org/1EbcC
However a simple getter will result in the indirection modification warning. It needs to return an array by-reference.
https://3v4l.org/DmDRp
Previous Comments:
------------------------------------------------------------------------
[2015-12-23 13:44:21] nowm at yandex dot ru
I've tested this behavior in PHP 5.5, 5.6, and 7 versions.
------------------------------------------------------------------------
[2015-12-23 13:42:24] nowm at yandex dot ru
Description:
------------
When a class has no "property" property and you are trying set it as $class->property = 'value', PHP is trying to use magic __set() method. It is correct behavior. But when you are trying to set it as $class->property['index'] = 'value', PHP ignores the setter, creating the "property" as a property of the class. It is incorrect behavior, because class had no "property" property, so it firstly have to use setter.
Test script:
---------------
<?php
class SetterTester {
public function __set($name, $value) {
echo 'Setter is used', PHP_EOL, PHP_EOL;
}
}
echo "Test normal version", PHP_EOL;
$test = new SetterTester();
$test->property = "Some value";
echo "Test array version", PHP_EOL;
$test = new SetterTester();
$test->property["index"] = "Some value";
Expected result:
----------------
Test normal version
Setter is used
Test array version
Setter is used
Actual result:
--------------
Test normal version
Setter is used
Test array version
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71203&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.