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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 11:01:33 2024 UTC