php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38109 visibility of property is processed incorrectly
Submitted: 2006-07-15 02:50 UTC Modified: 2006-07-15 10:20 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: public at grik dot net Assigned: helly (profile)
Status: Not a bug Package: SPL related
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: public at grik dot net
New email:
PHP Version: OS:

 

 [2006-07-15 02:50 UTC] public at grik dot net
Description:
------------
When I declare a property as "public", it's being ignored.
When I declare it as protected or private it is processed and visible from outside of the object.

Reproduce code:
---------------
A)
class Rules extends ArrayObject {
    public $len;
    function __construct($array){
        parent::__construct($array,2);
        $this['len'] = 2;
    }
}
$x = new Rules(array(1,2));
var_dump($x->len);

B)
class Rules extends ArrayObject {
    protected $len;
    function __construct($array){
        parent::__construct($array,2);
        $this['len'] = 2;
    }
}
$x = new Rules(array(1,2));
var_dump($x->len);

Expected result:
----------------
A) int(2)

B) Fatal error</b>:  Cannot access protected property Rules::$len

Actual result:
--------------
A) NULL

B) int(2)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-07-15 09:42 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The ",2" in the constructor call means ArrayObject::SPL_ARRAY_ARRAY_AS_PROPS which means use the array as properties if there is no conflict with visible properties. That is in case A) the property len is visible outside hence the property can be used. In case B) the property len is not used so that the array is being used instead. You will see an interesting behavior if you provide a class method in case B that dumps the contents.
 [2006-07-15 10:20 UTC] public at grik dot net
Thanks, then it's a documentation bug.

In the documentation there is no mention that it is about _not_visible_ properties only.

http://www.php.net/~helly/php/ext/spl/classArrayObject.html#f4077c9cb0ca99a6fc4d5da841443224
"Array indices can be accessed as properties in read/write."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 03:01:28 2024 UTC