New object with name in variable

Dvir Levy

I am trying to initiate an object with a dynamic path. I have the variable $model with the model name.

$model = "foo";
$class = new \Path\To\$model();

I get the error

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

If I try $class = new \Path\To\{$model}(); I get the error

 Parse error: syntax error, unexpected '{', expecting identifier (T_STRING)

When I try

namespace \App\Models
$class = new $model(); 

I get the error Class 'foo' not found

When I try $class = new \Path\To\foo(); it works.

Any ideas?

J. Doe

Try:

$class = "\Path\To\foo";
$object = new $class();

Or:

use Path\To\foo;
$class = foo::class;
$object = new $class();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Property name on object from variable

From Javascript

Javascript use variable as object name

From Dev

Algorithms new variable with same name

From Dev

New-Object : The term New-Object is not recognized as the name of a cmdlet

From Dev

Getting the object variable name in JavaScript

From Dev

duplicate the atrribute of an 'object' to a new variable

From Dev

Object name as variable in Logtalk

From Dev

Reference object variable name within object itself

From Dev

new object with brackets as property name

From Dev

Javascript object property with variable name

From Dev

Deserialize into object with variable name

From Dev

How to concatenate variable with an object name?

From Dev

jQuery- Use variable name in each loop to name new variable

From Dev

R : name of an object stored in a variable

From Dev

add new properties to object with variable name in mongoose

From Dev

Using variable *name* as property name in object literal

From Dev

Initializing object with name vs variable

From Dev

How to deserialize object to new object with the same name?

From Dev

How to use variable name in object?

From Dev

Javascript - Assign (not clone) a large object to a new variable to reduce its name

From Dev

Python, use variable as object "name"

From Dev

Referring to a new worksheet with variable name

From Dev

Create a new column filled by a variable name in python

From Dev

Assign object variable name to a variable javascript or typescript

From Dev

Dynamic variable name in object

From Dev

reference object name with variable - javascript

From Dev

invalid name - creating new variable

From Dev

how to use the variable as the name of an object?

From Dev

Saving object name in a new variable ansible

Related Related

  1. 1

    Property name on object from variable

  2. 2

    Javascript use variable as object name

  3. 3

    Algorithms new variable with same name

  4. 4

    New-Object : The term New-Object is not recognized as the name of a cmdlet

  5. 5

    Getting the object variable name in JavaScript

  6. 6

    duplicate the atrribute of an 'object' to a new variable

  7. 7

    Object name as variable in Logtalk

  8. 8

    Reference object variable name within object itself

  9. 9

    new object with brackets as property name

  10. 10

    Javascript object property with variable name

  11. 11

    Deserialize into object with variable name

  12. 12

    How to concatenate variable with an object name?

  13. 13

    jQuery- Use variable name in each loop to name new variable

  14. 14

    R : name of an object stored in a variable

  15. 15

    add new properties to object with variable name in mongoose

  16. 16

    Using variable *name* as property name in object literal

  17. 17

    Initializing object with name vs variable

  18. 18

    How to deserialize object to new object with the same name?

  19. 19

    How to use variable name in object?

  20. 20

    Javascript - Assign (not clone) a large object to a new variable to reduce its name

  21. 21

    Python, use variable as object "name"

  22. 22

    Referring to a new worksheet with variable name

  23. 23

    Create a new column filled by a variable name in python

  24. 24

    Assign object variable name to a variable javascript or typescript

  25. 25

    Dynamic variable name in object

  26. 26

    reference object name with variable - javascript

  27. 27

    invalid name - creating new variable

  28. 28

    how to use the variable as the name of an object?

  29. 29

    Saving object name in a new variable ansible

HotTag

Archive