1

I have a function that displays a table based on a set of records given the id of the records only. The function then calls the class by variable pulled from an array of parameters. This worked just fine...

function displayTable($arr) {
...
    foreach ($a['ids'] as $key => $arr) 
    {
        $m=$a['model'];
        $o = new $m($arr['id']);
    ...
    }
}

The issue is I now have the class in a namespace and the following does not work and throws an error...

function displayTable($arr) {
...
    foreach ($a['ids'] as $key => $arr) 
    {
        $m=$a['model'];
        $o = new \My\New\Namespace\$m($arr['id']);
    ...
    }
}

Parse error: syntax error, unexpected '$m' (T_VARIABLE), expecting identifier (T_STRING)

How can I accomplish this?

1 Answer 1

1

Just prepend namespace to variable, before object initialization:

$m = '\\My\\New\\Namespace\\' . $m;
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.