Bug #71203 [Com]: Ignoring class setter

[email protected] ("nowm at yandex dot ru")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=71203&edit=1

 ID:                 71203
 Comment by:         nowm at yandex dot ru
 Reported by:        nowm at yandex dot ru
 Summary:            Ignoring class setter
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 Operating System:   any
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

I've tested this behavior in PHP 5.5, 5.6, and 7 versions.


Previous Comments:
------------------------------------------------------------------------
[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.