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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 19:01:30 2024 UTC