Req #51808 [Opn->Csd]: Operator `new' does not support expressions for class name
| From: | cmb@php.net | Date: | Mon, 31 May 2021 14:51:00 +0000 |
| Subject: | Req #51808 [Opn->Csd]: Operator `new' does not support expressions for class name | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-234122@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=51808&edit=1
ID: 51808
Updated by: cmb@php.net
Reported by: m dot przybylski at bkfmyjnie dot pl
Summary: Operator `new' does not support expressions for
class name
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Class/Object related
Operating System: linux/current
PHP Version: 5.2.13
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
This is supported as of PHP 8.0.0[1].
[1] <https://3v4l.org/2bbfa>
Previous Comments:
------------------------------------------------------------------------
[2010-08-03 22:47:19] giorgio dot liscio at email dot it
does not works too:
new MyClass()->method();
(new MyClass())->method();
new ($className = FuncGetClassAsString())();
these are not really important to fix, but are welcome, in future maybe
------------------------------------------------------------------------
[2010-08-03 17:58:55] + at ni-po dot com
Imho this isn't obvious at all. At least your proposed syntax
new (func())()
isn't quite readable or understandable.
------------------------------------------------------------------------
[2010-05-13 12:53:31] m dot przybylski at bkfmyjnie dot pl
Description:
------------
Operator `new' accepts either class name as either symbol token or variable
containing class name (T_STRING or
T_VARIABLE), but does not allow expressions returning class name.
Test script:
---------------
/* first form, works */
$handler = new SomeClass($constructorParam);
/* second form, works */
$handlerClass = handlerFor($something); /* returns name of some class to be instantiated */
$handler = new $handlerClass($constructorParam);
/* third form -- does not work */
$handler = new (handlerFor($something))($constructorParam);
Expected result:
----------------
The third form ought to work just like the other two, so one doesn't need to
create extra variable for single use.
Actual result:
--------------
PHP Parse error: syntax error, unexpected '(', expecting T_STRING or T_VARIABLE
or '$'
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51808&edit=1