In Laravel (4 & 5), I am trying to use dynamic class names and then call them directly, but receiving a fatal error if not first storing them in a local string variable.
Assuming I have a basic class:
class SimpleModel {
private $modelName;
function __construct($id) {
$this->modelName = AnotherModel::getName($id);
}
}
Within the methods, I can easily do
$modelName = $this->modelName;
$modelName::find(1);
But I get a fatal error when trying the following:
$this->modelName::find(1);
This triggers a Symfony\Component\Debug\Exception\FatalErrorException with a message of
syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
Basically the T_PAAMAYIM_NEKUDOTAYIM is a syntax error generated from the :: symbol.
Can't wrap my head around why Laravel (or PHP in general) allows dynamic class names when using a local variable (within a method) but not a class variable.
I also tried putting it in a seprate method getModelName() method but getting the same error.
$this->getModelName()::find(1);
Instanciating a new class every time (new $this->modelName) is not a good solution.
I was looking into using PHP's Reflection but not sure how to do it without instanciating a new class every time.
Since it is working when using a local string, it seems as Reflection might be an over kill.