|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-11-03 17:36 UTC] gabriel at totoliciu dot ro
[2011-11-03 20:27 UTC] anon at anon dot anon
[2011-11-03 20:53 UTC] felipe@php.net
[2011-11-03 20:53 UTC] felipe@php.net
-Status: Open
+Status: Bogus
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 04:00:01 2025 UTC |
Description: ------------ I wanted to write something like this inside a method. DEFAULT_CLASS is a string containing a name of an existing class. The problem is that there is no way to instantiate an object using a class constant by using "new self::CLASS_CONSTANT"; This will generate a syntax error. I know that the workaround is to store the value of the class constant into a variable, but couldn't this be improved somehow? Test script: --------------- <?php class default_class { function do_nothing() { } } class TheParent { const DEFAULT_CLASS="default_class"; public function init() { $c=new self::DEFAULT_CLASS; } } Expected result: ---------------- I would expect to have an object instantiated with the class stored in the DEFAULT_CLASS constant. Actual result: -------------- Produces a syntax error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /some_path/test.php on line 14