php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63602 __callStatic does not see $this
Submitted: 2012-11-26 03:39 UTC Modified: 2012-11-26 06:10 UTC
From: stealz at op dot pl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.9 OS: Windows 7
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: stealz at op dot pl
New email:
PHP Version: OS:

 

 [2012-11-26 03:39 UTC] stealz at op dot pl
Description:
------------
When we call the method foo() of some class A in a static context inside the object B, the method can see $this as an object B. However, when method doesn't exist and __callStatic is triggered, $this is undefined. Why is that so?

Test script:
---------------
abstract class A {
	function foo() { # non-static on purpose
		var_dump($this);
	}
	static function __callStatic($method, $args) {
		var_dump($this);
	}
}

class B {
	function test() {
		A::foo();
		A::someMethod();
	}
}

$o = new B();
$o->test();

Expected result:
----------------
object(B)[4]
object(B)[4]

Actual result:
--------------
object(B)[4]
null

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-26 06:10 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2012-11-26 06:10 UTC] laruence@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

you should not use $this in a static method.

you should see a strict warning for this like:
PHP Strict Standards:  Non-static method A::foo() should not be called statically, 
assuming $this from incompatible context

for your question:
__callStatic does not parse the this_ptr for static calls
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 15:01:30 2025 UTC