|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53697 "new" keyword vs. call_user_func
Submitted: 2011-01-08 15:03 UTC Modified: 2016-01-16 02:41 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: pablick at gmail dot com Assigned: danack (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.3.5 OS: Irrelevant
Private report: No CVE-ID: None
 [2011-01-08 15:03 UTC] pablick at gmail dot com
When one defines a class with __callStatic() which under some circumstances 
accepts method name of "new", it may not be called. However, with __call(), it may 
be called. May be solved using call_user_func(), but that loses the nice syntax. 

Personally, I don't get it why "new" is parsed as T_NEW after "Base::" and not 
after $base->. 

Test script:
class Base {
	public static function __callStatic($method, $args) {
		if ($method == 'new')
			echo 'Base::new() called successfully';
	public function __call($method, $args) {
		if ($method == 'new')
			echo '$base->new() called successfully';

$base = Base::new(); 
call_user_func(array('Base', 'new');
$base = new Base;

Expected result:
Base::new() called successfully
Base::new() called successfully
$base->new() called successfully

Actual result:
Parse error: syntax error, unexpected T_NEW, expecting T_STRING or T_VARIABLE or 
'$' in C:\inetpub\wwwroot\test.php on line 14


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-08 15:14 UTC] pablick at gmail dot com
The importance of this bug is just in simplicity: If one wants to have a static method named "new", that will be creating instances (singletons or 
any other controlled process of instantiation), it's more convenient to have a class method named "new" in the API docs rather than having to type 
 [2011-07-22 08:36 UTC] jille at hexon dot cx lists "new" as being a reserved keyword. That implies you shouldn't call (static or non-static) methods "new".
 [2016-01-16 02:41 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: danack
 [2016-01-16 02:41 UTC]
This was fixed in PHP 7 -
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 15 15:01:29 2024 UTC