php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70554 trait implementing __call is called for static magic method of 'use'ing class
Submitted: 2015-09-22 20:48 UTC Modified: 2015-09-25 11:37 UTC
From: paul at salesintel dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.6.13 OS: Windows 10
Private report: No CVE-ID: None
 [2015-09-22 20:48 UTC] paul at salesintel dot com
Description:
------------
A class that uses a trait that implements __call, and then from an instance method calls a static method that does not exist, ends up calling __call in trait rather than __callStatic.

Test script:
---------------
trait TestTrait {
	function __call($name, $args)
	{
		echo "instance method '$name' called.\n";
	}

	static function __callStatic($name, $args)
	{
		echo "static method '$name' called.\n";
	}
}

class TestClass {
	use TestTrait;

	function test() {
		// this ends up calling __call method in TestTrait ?!?!?! WTF?
		TestClass::magic_static();
	}
	static function staticTest()
	{
		// this calls __callStatic as expected
		TestClass::magic_static();
	}
}

class TestClass2 {
	function __call($name, $args)
	{
		echo "instance method '$name' called.\n";
	}

	static function __callStatic($name, $args)
	{
		echo "static method '$name' called.\n";
	}

	function test() {
		// this calls __callStatic as expected
		TestClass::magic_static();
	}
	static function staticTest()
	{
		// this calls __callStatic as expected
		TestClass::magic_static();
	}
}


$t = new TestClass();
$t->test();
$t->staticTest();

$t = new TestClass2();
$t->test();
$t->staticTest();


Expected result:
----------------
See inline comments in sample code

Actual result:
--------------
See inline comments in sample code

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-23 02:57 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2015-09-23 02:57 UTC] laruence@php.net
it's have been explained many times in bugs :)

first of all , :: doesn't means "static" call. like:

parent::__construct();

php using calling scope to determinate whether it is a static call or not.

thus, in you example,  when you calling TestClsss::magic_static() in test; it has a calling scope of Testclass instance, thus it's not considered as a static call

so I am going to mark this as not a bug.

thanks
 [2015-09-25 11:37 UTC] mike@php.net
Please see http://php.net/forward_static_call
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 15:01:30 2024 UTC