php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31095 Nonstatic function can be called as if is static
Submitted: 2004-12-14 23:59 UTC Modified: 2005-02-28 21:14 UTC
From: levi at alliancesoftware dot com dot au Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.2 OS:
Private report: No CVE-ID: None
 [2004-12-14 23:59 UTC] levi at alliancesoftware dot com dot au
Description:
------------

  If you call a  member function in another class, it succeeds and $this is set to the *calling* class. The new PHP 5 object model should make the function inaccessible, unless
  [a] It is a static function ($this should be unset)
  [b] It is an ancestor class of the calling class ($this should be set)
  [c] PHP 4 compatibility is on ($this should be set)

At the very minimum, this should generate an error in strict mode.


  This may have been the expected behaviour in PHP 4, but PHP 5 has a static keyword for exactly this situation now. Note: This was reported in PHP4 under bugs #19384 and #12622 and was (incorrectly) claimed to have been fixed in #19194 (it still occurs in 4.3.4)

Note: Currently, $this is unset if
  [a] you call a global (non class) function
  [b] otherClassMethod() is declared static



Reproduce code:
---------------
#!/usr/local/bin/php -qC
<?
error_reporting(E_ALL | E_STRICT);

class OtherClass {
    public function otherClassMethod() {
        echo "Called otherClassMethod() from within class ".
               get_class($this)."\n";
    }
}

class NormalClass {
    public function __construct() {
        OtherClass::otherClassMethod();
    }
}

new NormalClass();

?>

Expected result:
----------------
PHP Notice:  Undefined variable:  this in testStatic2.php5 on line 7

(or even better):

PHP Notice: Calling nontstatic function as static in testStatic2.php5 on line 14

Actual result:
--------------
Called otherClassMethod() from within class NormalClass

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-28 21:14 UTC] sniper@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC