I am autoloading my classes and want a way to dynamically instantiate the class when used.
Instead of having 20 class instantiations in my parent class, I want a way to instantiate a class when its being called.
for example:
$this->template->render();
will instantiate
$this->template = new Template();
I attempted this
public function __set($name, $value)
{
return $this->$name;
}
public function __get($name)
{
$this->$name = new $name();
}
This doesn’t seem to work, but I also think I’m doing it wrong.
One issue that I can’t figure out also is that my classes reside in the \System namespace. I can’t seem to tack on new "\System".$name() or new \System.$name() without getting an error;
1 Answer