php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51808 Operator `new' does not support expressions for class name
Submitted: 2010-05-13 12:53 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: m dot przybylski at bkfmyjnie dot pl Assigned:
Status: Open Package: Class/Object related
PHP Version: 5.2.13 OS: linux/current
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: m dot przybylski at bkfmyjnie dot pl
New email:
PHP Version: OS:

 

 [2010-05-13 12:53 UTC] 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 '$'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-03 17:58 UTC] + 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-08-03 22:47 UTC] 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
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Feb 20 05:01:26 2019 UTC