php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71203 Ignoring class setter
Submitted: 2015-12-23 13:42 UTC Modified: 2015-12-23 18:15 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nowm at yandex dot ru Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
 [2015-12-23 13:42 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-23 13:44 UTC] nowm at yandex dot ru
I've tested this behavior in PHP 5.5, 5.6, and 7 versions.
 [2015-12-23 18:15 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-12-23 18:15 UTC] requinix@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 17:01:29 2024 UTC