php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46570 __get() behaves unpredictably
Submitted: 2008-11-14 01:53 UTC Modified: 2008-11-14 21:47 UTC
From: ms419 at freezone dot co dot uk Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.6 OS: Debian
Private report: No CVE-ID: None
 [2008-11-14 01:53 UTC] ms419 at freezone dot co dot uk
Description:
------------
I have a class (in the reproduce code "Test") which is basically a container for some attributes. Some of the attributes are the results of expressions which I only want to evaluate when the attribute is accessed for the first time. The __get() function seems perfect for this.

One attribute is "repository" - its value should be the result of an expression, concatenated with a slash character and another attribute ("name"), if "name" is not null.

When "name" is not null, the first time "repository" is accessed, __get() returns the result of the expression, without anything concatenated. The second time "repository" is accessed, __get() returns just a slash character and "name", without the result of the expression.

Reproduce code:
---------------
http://cgi.sfu.ca/~jdbates/tmp/php/200811130/test.phps

Expected result:
----------------
ket% php test.php                                                  
string(9) "some expr/name"
string(4) "some expr/name"
ket% 


Actual result:
--------------
ket% php test.php                                                  
string(9) "some expr"
string(4) "/name"
ket% 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-14 06:36 UTC] lbarnaud@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is because you are fetching $this->repository in __get(). This calls __get() inside of __get(), which returns null.
Use $this->values[$name] instead in __get().
 [2008-11-14 21:47 UTC] ms419 at freezone dot co dot uk
Thank you for explaining, lbarnaud. After some more testing, I confirmed that calling __get(<name>) inside __get(<name>) does indeed return null, although __get(<otherName>) inside __get(<name>) does not - it returns the value as expected.

I would like to change this bug report to a feature/change request. I want __get(<name>) inside __get(<name>) to get called as expected. My own __get() function already guards against infinite recursion. By not calling the function and returning null instead, PHP is unnecessarily protecting me from doing something which I actually want to do...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC