Bug #60214 [Opn->Bgs]: can't instantiate using class constant
| From: | felipe@php.net | Date: | Thu, 03 Nov 2011 20:53:21 +0000 |
| Subject: | Bug #60214 [Opn->Bgs]: can't instantiate using class constant | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-164858@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60214&edit=1
ID: 60214
Updated by: felipe@php.net
Reported by: gabriel at totoliciu dot ro
Summary: can't instantiate using class constant
-Status: Open
+Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: 2.6.34-gentoo-r6 GNU/Linux
PHP Version: 5.3SVN-2011-11-03 (snap)
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
At least currently it's not even intended to work. Use the workaround instead.
Previous Comments:
------------------------------------------------------------------------
[2011-11-03 20:27:01] anon at anon dot anon
As a simple workaround you can do:
$c = self::DEFAULT_CLASS;
$c = new $c();
------------------------------------------------------------------------
[2011-11-03 17:36:28] gabriel at totoliciu dot ro
I forgot to mention that you have to run Parent::init();
------------------------------------------------------------------------
[2011-11-03 17:34:59] gabriel at totoliciu dot ro
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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60214&edit=1