Reputation: 7728
I want to do this:
private static function parserLib() {
return USE_XML ? 'XmlParser' : 'IdfParser';
}
static function create($arr = []) {
return new ${self::parserLib()}($arr);
}
I've tried with this:
new ${'XmlParser'}($test);
Don't work.
This works:
$var = 'XmlParser';
new $var($test);
Why is not possible to use the {}
to use the value retrieved from a method?
Upvotes: 2
Views: 42
Reputation: 670
$var = 'XmlParser';
new $var($test);
You define variable $var and used it value to instance class.
new ${'XmlParser'}($test);
${'XmlParser'} is means get value of variable XmlParser, but you have not such variable.
You try get value of undefined variable and in the result got such error "PHP Notice: Undefined variable: XmlParser".
If you define variable XmlParser it will be works
class XmlParser {}
$XmlParser = 'XmlParser';
new ${'XmlParser'}($test);
Also please read about php string complex syntax
Upvotes: 1