Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

so I have some namespaces defined and I wand to instance new objects like this:

$this->controller = new controller$this->controller($this->cfg);

but I get a

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

How can I make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
139 views
Welcome To Ask or Share your Answers For Others

1 Answer

It looks like you're trying to dynamically determine the class name of the object to instantiate. To do this, you can create a variable that contains a string with the class name, and then instantiate on that variable:

$string = $someObject->someMethod();
$class = '\controller\' . $string;
$newObject = new $class();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...