php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36707 $this visibility violated
Submitted: 2006-03-12 19:44 UTC Modified: 2006-03-15 12:43 UTC
From: andreigurin at tiscali dot it Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.4.2 OS: windows xp sp2 / linux centos /
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: andreigurin at tiscali dot it
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-13 09:13 UTC] judas dot iscariote at gmail dot com
your test results :

Fatal error: Using $this when not in object context..

and the code you mention cannot run in PHP4, since PHP doesn't have public, private modifiers...

what are you talking about ?
 [2006-03-13 09:18 UTC] andreigurin at tiscali dot it
Look here: This happens in 4.xxx versions, and in 5.xxx, my code is for version 5.
I reported the first version i tested it. It equally falls on 4 an on 5.
 [2006-03-13 09:19 UTC] tony2001@php.net
Works fine both with 4.x and 5.x.
No bug here.
 [2006-03-13 10:34 UTC] andreigurin at tiscali dot it
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();

?>
 [2006-03-13 10:37 UTC] tony2001@php.net
This is expected and it was done in order to keep compatibility with 4.x.
 [2006-03-13 10:42 UTC] andreigurin at tiscali dot it
Are there a piece of documentation describing this behavior? Thanks for reply!
 [2006-03-13 14:26 UTC] andreigurin at tiscali dot it
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!!!*/
?>
 [2006-03-13 14:29 UTC] andreigurin at tiscali dot it
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();
?>
 [2006-03-13 14:30 UTC] tony2001@php.net
It's not related to public/private modifiers at all.
Static methods which were not declared as static inherit $this from the parent scope.
This is the only way to preserve backward compatibility with PHP4 and it won't change in PHP5.
 [2006-03-14 09:09 UTC] andreigurin at tiscali dot it
You say : "Static methods which were not declared as static inherit $this from the parent scope". Did you say "PARENT SCOPE"???

Well, show me in the above code something like "class Bar extends Foo"... Foo isn't an ancestor for Bar class, and so Bar must not inherit $this from it. It's violating the OO paradigma, and it's a bug.

As ask you once again to point me to the documentation describing such a behavior and, most of all, explaining it.

Thanks!!!!
 [2006-03-14 09:17 UTC] tony2001@php.net
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.
 [2006-03-15 12:43 UTC] dmitry@php.net
Added E_STRICT warning in CVS HEAD and PHP_5_1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 06:01:27 2024 UTC