ID: 49121 User updated by: jay at jay dot cz Reported By: jay at jay dot cz Status: Open Bug Type: Class/Object related Operating System: ubuntu linux PHP Version: 5.2.10 New Comment:
"Private limits visibility only to the class that defines the item." It is not (static) FOO that defines that private item, but it is $foo instance that defines it. If it is a "feature", then it is pretty big hole, that static class has full access to all properties of all instances created from it. And if it is meant, that it's not limited by "instance" but by "class", then also two instances from the same class should be able to access each others private properties. This behavior was reported also in comment http://php.net/manual/en/language.oop5.visibility.php#69104 Previous Comments: ------------------------------------------------------------------------ [2009-07-31 12:42:08] jay at jay dot cz Description: ------------ Private property of initialized class, is accessible from static version of the same class. Reproduce code: --------------- class FOO { private $bar = 0; private static $instance = false; public static function get_instance(){ if(self::$instance === false) self::$instance = new FOO; return self::$instance; } public static function set_bar( $b ){ $foo = FOO::get_instance(); $foo->bar = $b; } public static function dump_bar(){ $foo = FOO::get_instance(); echo $foo->bar; } } FOO::set_bar( 1 ); FOO::dump_bar(); $foo = FOO::get_instance(); echo $foo->bar; Expected result: ---------------- Fatal error: Cannot access private property FOO::$bar in /var/www/index.php on line 10 I should not be able to write to a private property from "outside". (Even thou it's the same code for both classes (static and initialized).) Actual result: -------------- 1 Fatal error: Cannot access private property FOO::$bar in /var/www/index.php on line 20 Well, I can write to the private property and I can read it, from inside the static. The error is when I try to access it from completely "outside". And that error is correct. Same result for private and protected. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49121&edit=1
