#49121 [Opn->Fbk]: private var accessible from static class
ID: 49121
Updated by: jani@php.net
Reported By: jay at jay dot cz
-Status: Open
+Status: Feedback
Bug Type: Class/Object related
Operating System: ubuntu linux
PHP Version: 5.2.10
New Comment:
Can you please explain what the bug is you're reporting? So far
everything here works just like they should. And that comment you
referred to is about totally different thing.
Previous Comments:
------------------------------------------------------------------------
[2009-07-31 14:13:48] jay at jay dot cz
"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)
------------------------------------------------------------------------
[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
Thread (3 messages)