#50822 [Bgs]: Private instance variable visibile for new instance created within an object
| From: | aron at zajlik dot hu | Date: | Fri, 22 Jan 2010 15:16:50 +0000 |
| Subject: | #50822 [Bgs]: Private instance variable visibile for new instance created within an object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-146873@lists.php.net to get a copy of this message | ||
ID: 50822
User updated by: aron at zajlik dot hu
Reported By: aron at zajlik dot hu
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Mac OS X
PHP Version: 5.3.1
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2010-01-22 14:38:05] 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 12:45:36] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50822&edit=1