php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42021 Intance of same class, access directly private vars, dont using __get() method
Submitted: 2007-07-17 21:17 UTC Modified: 2007-07-31 21:21 UTC
From: o_gangrel at hotmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.3 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: o_gangrel at hotmail dot com
New email:
PHP Version: OS:

 

 [2007-07-17 21:17 UTC] o_gangrel at hotmail dot com
Description:
------------
In a class, if instance this same class inside, the method __get() does not is called.

Reproduce code:
---------------
class class_test {

	private $sub_classes = array();
	private $foo = "bar"; // Default value

	public function __construct($n = 0)
	{
		for($i = 0; $i < $n; $i++) {
			$this->sub_classes[] = new class_test(); // New "sub" instaces
		}
		if($n > 0) {
			$this->foo = $n; // Change the default value on main instace
		}
	}

	public function __get($name)
	{
		switch($name) {
			case 'foo':
				return '__get("foo") = '.$this->foo; // Getting the variable, changing the value
			break;

			case 'subs':
				$subs = '$this->foo = '.$this->foo."\n"; // Value of var on main instace
				foreach($this->sub_classes as $key => $sub_class) {
					$subs.= "\$this->sub_classes[$key]->foo = ".$this->sub_classes[$key]->foo."\n"; // The value of variable on each "sub" instace
				}
				return $subs;
			break;
		}

		return false;
	}
}

$test = new class_test(2); // Create the main instace, with 2 "sub" instaces

echo $test->subs; // Show the variables

Expected result:
----------------
$this->foo = 2
$this->sub_classes[0]->foo = __get("foo") = bar
$this->sub_classes[1]->foo = __get("foo") = bar


Actual result:
--------------
$this->foo = 2
$this->sub_classes[0]->foo = bar
$this->sub_classes[1]->foo = bar


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-17 22:12 UTC] johannes@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 visibility modifiers work on a per class basis and is not rejected to the same object.
 [2007-07-17 22:36 UTC] johannes@php.net
Should have been "restricted" instead of "rejected"...
 [2007-07-18 15:33 UTC] o_gangrel at hotmail dot com
Sou you mean, when I have an instace of the class itself, the method __get() will not called? It not sounds good to mee! And the variable is a PRIVATE variable, and it can't be accessed only becouse the class is the same and the instace isn't.
 [2007-07-31 21:21 UTC] o_gangrel at hotmail dot com
Unfortunately an other bug I report, and have no attencion... and other will (may be from the PHP crew) report and solve the bug.

Well... every thing I report is a bogus... then I will just use de PHP and not report, only pray, to the "bogus" in future have solved.

Good luck.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 29 15:00:02 2025 UTC