php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50822 Private instance variable visibile for new instance created within an object
Submitted: 2010-01-22 12:45 UTC Modified: 2010-01-22 22:51 UTC
From: aron at zajlik dot hu Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.1 OS: Mac OS X
Private report: No CVE-ID: None
 [2010-01-22 12:45 UTC] aron at zajlik dot hu
Description:
------------
I found a situation which to me seems like unexpected behavior. I 
private variable (as far as I know) should only be accessible within the 
object itself, and not by the "outside world". Yet this becomes a bit 
confusing once the object is created within an instance of itself. Is 
this a bug or is this expected behavior?

Reproduce code:
---------------
<?
class Test{
	private $variable = "asdf";
	
	static function create_a_test(){
		$tobj = new Test();
		echo $tobj->variable;	// Works! (which it shouldn't?!?)
	}

}

$my_test = new Test();
//echo $my_test->variable;	// Returns Fatal error: Cannot access private property (which it should!)
$my_test->create_a_test();

?>

Expected result:
----------------
I would expect the above code NOT to return the private variable on line 
7.

Actual result:
--------------
The private variable can be accessed without a problem.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-22 14:38 UTC] jani@php.net
Ever heard of "singleton" ? Also: 

  http://php.net/manual/en/language.oop5.patterns.php

So yes, this is expected and correct behaviour.
 [2010-01-22 15:16 UTC] aron at zajlik dot hu
Although my example (by chance) is similar to a singleton pattern, my 
goal was actually very different. But regardless, there is no reason 
why the object $my_test should be able to directly access a private 
variable of $tobj. Even if it were a singleton class this would not be 
necessary. 

To say my point in a different way:
- In a typical singleton pattern, the instantiated object is a private 
property of the singleton class, so there is no problem accessing that 
from the class. In fact, you are able to access public properties and 
methods of the instantiated object from within the singleton class.
- BUT there is no reason why you should be able to access a private 
property of that instance! Only the instance itself should have access 
to that...

Hope I made myself clear...

peace,
aron
 [2010-01-22 22:51 UTC] degeberg@php.net
The reason why you make methods and properties private in an object is to encapsulate things and to hide implementation specific details from the outside world, so to speak. However, it is expected that an instance of Test knows how Test works internally and therefore you are able to access private properties of other instances of Test.

Test knows how Test works and Test knows there is a property called $variable in Test. It's pointless hiding the implementation of Test from Test.

You'll find that similar behavior is possible in other object oriented languages (Java for instance).
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Feb 19 13:00:01 2026 UTC