php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49121 private var accessible from static class
Submitted: 2009-07-31 12:42 UTC Modified: 2009-08-08 01:00 UTC
From: jay at jay dot cz Assigned:
Status: No Feedback Package: Class/Object related
PHP Version: 5.2.10 OS: ubuntu 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: jay at jay dot cz
New email:
PHP Version: OS:

 

 [2009-07-31 12:42 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-31 14:13 UTC] 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 23:32 UTC] jani@php.net
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.
 [2009-08-08 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 11 18:01:28 2024 UTC