|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-03-12 19:44 UTC] andreigurin at tiscali dot it
Description:
------------
$this keyword visibility is violated if accessed from a function external to a class. This happens in 4.xxx versions, and in 5.xxx, Tested on Window 2000, Windows Xp sp2, Linux Centos(3.7?), Red hat, and some other Linux based systems
Reproduce code:
---------------
class Foo {
private $__prv;
public function __constructor($v) {
$this->__prv = $v;
}
public function do_something() {
bla bla bla...
hack();
}
public function prv() {
return $this->__prv;
}
}
function hack() {
$this->__prv = 'A very bad value!';
}
$foo = new Foo('A good __prv value');
$foo->do_something();
print $foo->prv();
Expected result:
----------------
Triggered error:
bla bla bla undefined variable this in file.php on line ...
Output:
A good __prv value
Actual result:
--------------
Output:
A very bad value
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 10:00:01 2025 UTC |
ok, try this <?php error_reporting(E_ALL); class Foo { protected $__prv; public function Foo($v) { $this->__prv = $v; } public function prv() { return $this->__prv; } function tst() { Bar::babar(); } } class Bar { public function babar() { print $this->prv(); } } $foo = new Foo('PRV VALUE'); $foo->tst(); ?>Well, IMHO, such a behavior is violating the basics of OOP... Documentation is missing(am I right?). This is a BUG (a feature??? mmm...). In php4 (private & public modifiers missing), you may access every object field... <?php error_reporting(E_ALL); class Foo { protected $__prv; public function Foo($v) { $this->__prv = $v; } public function prv() { return $this->__prv; } function tst() { Bar::babar(); } } class Bar { public function babar() { $this->__prv = 'a very very bad value'; /* IS IT OK???!!!*/ print $this->prv(); } } $foo = new Foo('PRV VALUE'); $foo->tst(); /* $foo->__prv is modified now!!!*/ ?>sorry, here is the php 4 code <?php error_reporting(E_ALL); class Foo { var $__prv; function Foo($v) { $this->__prv = $v; } function prv() { return $this->__prv; } function tst() { Bar::babar(); } } class Bar { function babar() { $this->__prv = 'a very very bad value'; print $this->prv(); } } $foo = new Foo('PRV VALUE'); $foo->tst(); ?>I didn't say a word about class inheritance. function foo() { //parent scope bar(); //child scope inside } This is what I mean. Feel free to invest some time into improving the documentation and explaining to others why it works the way it works. Please contact phpdoc@lists.php.net with all further questions. And stop reopening the report, there is no bug.