PHP Forced Parent
Sat, 14 Aug 2010I'm uncertain if this type of thing is necessary, or even a good idea. But I think forcing an empty variable with a reference to its parent could useful be for a framework or something that should be coupled for security. Since I have no arguments to pass in the construct I have to set a variable to apply from the Controller, in this case I just leave it NULL which really isn't used for anything.
class Model
{
public function __construct(Controller $forceParent = NULL)
{
/**
* Code here
*/
}
}
If the Model is called within a Controller, and it does not extend the Controller, which in my opinion it shouldn't, we must have a public constructor. So to keep the Model from being called elsewhere, which I doubt it ever would unless it was human error and misunderstanding, we can force it to reference the Controller object and place a meaningless NULL variable so that it must be called from the Controller object.
I was up late writing this and perhaps it's a big misunderstanding and there is a better way to do this. Why couple things up when i don't have to? I think because I don't want this Model to be used for anything, anywhere besides the Controller -- isn't that bad practice? Maybe, but I'm using this solely for an MVC pattern and will not use this Model object outside of the MVC. And if I so choose, the libraries that are called from within the Model can be used independently.