php.net |  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
Votes:1
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
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: pablick at gmail dot com
New email:
PHP Version: OS:

 

 [2011-01-08 15:03 UTC] pablick at gmail dot com
Description:
------------
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:
---------------
<?php
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;
$base->new();
?>

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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 
ClassName::getNewInstance().
 [2011-07-22 08:36 UTC] jille at hexon dot cx
http://www.php.net/manual/en/reserved.keywords.php 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] danack@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: danack
 [2016-01-16 02:41 UTC] danack@php.net
This was fixed in PHP 7 - https://3v4l.org/clKJj
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 17:01:33 2024 UTC