I have a desire to store an object as a property of a class. I then want to be able to call static methods on that class by directly referencing the property.
Consider the following:
class myModel {
public static function all()
{
return 1;
}
}
class myClass {
public $models;
public function __construct()
{
$this->models->myModel = new myModel;
$results = $this->models->myModel::all();
}
}
$result = new myClass;
PHP fails on $results = $this->models->myModel::all();
If I modify this by assigning the class property to a local variable, things work as expected:
$myModel = $this->models->myModel;
$results = $myModel::all();
I'd like to eliminate the need to assign the class property to a local variable.
Is this possible in PHP 5.3.x (the version I'm using) or indeed, in later versions?
The solution should be readable and the purpose of the code should not be otherwise obfuscated.
For clarity, this is a dramatic simplification of the working code. The benefits of storing the object in a class property are not evident in the example code I have posted.