Looking to see if there is any way to implement the kind of functionality of the __get() magic method, but when trying to instantiate a new class.
My goal is to have
$foo = new Cat();
Ultimately result in the creation of a generic object, i.e.
new Mammal('Cat');
Such that $foo would be an instance of the class Mammal, with the called class name ('Cat') passed as an argument to Mammal‘s constructor.
Note:
The end game I have in mind, for those familiar with Lithium or CakePHP, is to avoid having to define a whole bunch of classes for each different database table. If I did, most would simply be empty, the basic CRUD operations being all that is necessary. Plus, all those includes and class definitions can’t be great for overhead. My idea would be to use a single “Model” class to handle most of the generic functions, and I could always create subclasses if I needed more advanced functionality for a specific database table.
I don’t think there is any straightforward non-hacky way to achieve what you want with the class instantiation mechanism.
I would recommend using a factory:
Which would then work out what needs to be done behind the scenes. This also has the benefit of if you ever decide Cat requires its own class, you can simply modify the factory and you may not need to modify the code that is using it.