php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30002 Undefined property notice appear when __get method access $this->OtherProperty
Submitted: 2004-09-06 18:51 UTC Modified: 2005-05-19 13:26 UTC
Votes:5
Avg. Score:4.6 ± 0.5
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:2 (40.0%)
From: orlum at mail dot ru Assigned: andi (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2005-03-07 OS: *
Private report: No CVE-ID: None
 [2004-09-06 18:51 UTC] orlum at mail dot ru
Description:
------------
When __get method accesses other property in some class, expected call to __get method not occurs, undefined property notice appears and null value of property returns.

Reproduce code:
---------------
<?PHP

class A
{
	public function __get($property)
	{
		echo "__get()\n";

		if ($property == "B")
			return 1;
		elseif ($property == "C")
			return $this->B;
	}
}


error_reporting(E_ALL);

$a = new A();
echo "B={$a->B}\n";
echo "C={$a->C}\n";


?>

Expected result:
----------------
__get()
B=1
__get()
__get()
C=1


Actual result:
--------------
__get()
B=1
__get()
Notice:  Undefined property:  A::$B in PHPDocument1 on line 12
C=


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-06 21:46 UTC] helly@php.net
Interesting thing:

1) It should happen at all because using $this->B should result in an implicitly declared proeprty.

2) It is expected behavior because __get/__set have a simple recursion protection which disables __get/__set during __get/__set calls.
 [2005-02-12 18:06 UTC] imperior at op dot pl
Another exaple, where IMHO it should work:
<?php
class Creator {
    public $objects;
    
    public function __get($name) {
        if (!isset($this->objects[$name])) {
            $this->objects[$name] = new $name($this);
        }
        return $this->objects[$name];
    }
    
}

class Class1 {
    public function __construct($Creat) {
        echo 'Class1';
        $Creat->Class2;
    }    
}

class Class2 {
    public function __construct($Creat) {
        echo 'Class2';
    }    
}

$Creat = new Creator;
$Creat->Class1;
?>

OUTPUT:
Class1
Notice: Undefined property: Creator::$Class2 in D:\Server\www\noname\test.php on line 17

Expected:
Class1Class2
 [2005-04-22 13:18 UTC] tony2001@php.net
I really doubt that it's a bug because if we allow calling __get() from __get() you'll get into endless loop easily, which is of course much worse than just a notice.
Take a look into zend_std_read_property() (in zend_object_handlers.c), there is simple loop protection.
So I'd prefer more to see this documented than fixed in some way.
 [2005-05-19 13:26 UTC] stas@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

__get is not recursive - i.e., fetches happening in __get would not invoke __get again for the same object. Indeed, this is loop protection and also feature allowing __get to access/create properties that other methods would use __get for. 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 05:01:33 2024 UTC