php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35831 Call a public function like a static function with a bogus $this var
Submitted: 2005-12-28 21:02 UTC Modified: 2005-12-28 21:07 UTC
From: francois dot levasseur at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.1.1 OS: Gentoo Linux (2.6)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: francois dot levasseur at gmail dot com
New email:
PHP Version: OS:

 

 [2005-12-28 21:02 UTC] francois dot levasseur at gmail dot com
Description:
------------
I can call a public function like a static function from another class. The variable "$this" refers to the caller.

Reproduce code:
---------------
<?php

class testA{
        private $var;

        public function __construct(){
                $this->var = 'Hello';
        }

        public function getVar(){
                return $this->var;
        }

        public function test(){
                return $this->getVar();
        }
}

class testB{

        public function test(){
        // Should not work!
                echo testA::test();
        }
}

$myTestB = new testB();
$myTestB->test();
?>


Expected result:
----------------
Error because class testB called a static function that does not exists.

Actual result:
--------------
Call the static method "getVar()" in class testB. $this refers to testB.

PHP Fatal error:  Call to undefined method testB::getVar() in /var/www/svnhome/kronos/kronoscrm/test.php on line 15

Fatal error: Call to undefined method testB::getVar() in /var/www/svnhome/kronos/kronoscrm/test.php on line 15



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-28 21:07 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

To remain backward compatible calling dynamic functions statically still works.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 06:01:27 2025 UTC