2

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?

1 Answer 1

1
$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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.