I've kind of been experimenting around with some different ways of dependencies, inheritance and scoping. No way of doing it is set in stone yet, I'm just experimenting so I would like to have an answer to the actual problem I'm facing or to know if I'm wanting the impossible. I don't want advice on how to build a standard MVC. The point of this experimenting is to find out if there really aren't better ways of doing very specific things.
Necessary information
I have about 8 'core' classes which I basically want to be available everywhere, even within each other (like siblings). This would likely create circular dependency, which is never what we want.
The thing is that I want all of these classes to come together as the "core", meaning I want all of them to share one object and modify situations for each other (kind of like a singleton pattern, I guess?). Then within the MVC itself, the classes will extend this core and those will be dependency-injected and such as any other MVC would.
The core is quite small. It just has some very basic functions like loading classes, being able to set a few things to $this
, compiling the templates and such things.
So, I've been having this tiny little train of thoughts, what if I extended said 8 in a "train" type manner, and call only the very last extension so that I have them all within 1 single instance? This would look like this:
class base{
//The base
}
class model extends base{
//The 1st child
public function load($modelName){
//Code to load a model
}
}
class controller extends model{
//2nd child
public function load($controllerName){
//code to load controller
}
}
Great, I have everything in one instance!
Well, not exactly how I'd like it to be, though!
This brings out a big issue: Calling any "duplicate" function like load()
will cause the last one to run by default.
$this->load('mymodel');
//Returns: controller "mymodel" not found
How I would like to call it is as follows;
$core->model->load('mymodel');
//Executes the childclass Model's "load" method
$core->controller->load('mycontroller');
//Executes the controller class's load method.
The real question:
Can I achieve this? If so, how?
I was thinking of something like this:
1 ) construct in the base
2 ) get all the child classes
3 ) put child classes within variables through $this->{$className} = $classObj;
4 ) call $this->classname->method style.
But I simply can't think of any way to achieve this. Googling it only results in people asking about extending multiple parents...