Req #53697 [Opn->Csd]: "new" keyword vs. call_user_func

From: Date: Sat, 16 Jan 2016 02:41:50 +0000
Subject: Req #53697 [Opn->Csd]: "new" keyword vs. call_user_func
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198706@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53697&edit=1 ID: 53697 Updated by: danack@php.net Reported by: pablick at gmail dot com Summary: "new" keyword vs. call_user_func -Status: Open +Status: Closed Type: Feature/Change Request Package: Class/Object related Operating System: Irrelevant PHP Version: 5.3.5 -Assigned To: +Assigned To: danack Block user comment: N Private report: N New Comment: This was fixed in PHP 7 - https://3v4l.org/clKJj Previous Comments: ------------------------------------------------------------------------ [2011-07-22 08:36:36] 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". ------------------------------------------------------------------------ [2011-01-08 15:14:58] 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-01-08 15:03:33] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=53697&edit=1

« previous php.bugs (#198706) next »