Edit report at https://bugs.php.net/bug.php?id=60214&edit=1

 ID:                 60214
 Comment by:         gabriel at totoliciu dot ro
 Reported by:        gabriel at totoliciu dot ro
 Summary:            can't instantiate using class constant
 Status:             Open
 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:

I forgot to mention that you have to run Parent::init();


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to