Reputation: 2821
I am working on a custom framework and have a couple of questions about parent class instantiation through child classes.
I will paste some code snippets and then get into the questions
class CFrameWork {
private $applicationName = "Application Name";
function __construct($instance = "development") {
echo "Hello, I am the Parent and I have been constructed<BR />";
}
public
function startApplication() {
$this->checkMaintenanceMode();
if (!isset($_GET['query'])) {
$this->intialize();
} else {
// Call the appropriate controller
// Method and function are pulled from the query
// Code not displayed
// EDITS BELOW
if (method_exists($method, $function)) {
try {
call_user_func(array(new $method, $function), $this);
} catch (CFException $exp) {
$this->show404();
exit;
}
} else {
$this->show404();
exit;
}
}
}
}
Next we have a controller class
class childController extends CFrameWork {
function index() {
echo "Index Controller";
}
function register() {
echo "Registration Controller";
}
}
Now in the index.php, I have
$application = new CFrameWork();
$application->startApplication();
The way this Framework interprets queries:
localhost/childController/index - Calls the index() function in class childController localhost/childController/register - Calls the register() function in class childController
and so on...
So here's my concern. The parent class in this framework in instansiated twice each time a controller method is called. Once by the index.php (where the initial application is created) and then by the controller when it is extended. IN other words, CFrameWork::__construct() is created again everytime a controller method is reached.
My questions:
1) Does this have any harmful effect?
2) Can this be avoided?
3) Any suggestions on how you would do this differently?
Upvotes: 0
Views: 795
Reputation: 58444
This is bad on several levels:
Create class that deals exclusively with routing. It will be the one which deals with $_GET['query']
. Give that class a method getControllerName()
.. or something and getControllerMethod()
. Does not matter how you call them. What matters is that the result of thous methods you use to create the new Controller instance right there in the index.php
and perform an action on that controller.
Besides what's written in 2., you should learn what SOLID principles are, what Law of Demeter is, what is dependency injection. And only then try to make an MVC framework again.
Upvotes: 0
Reputation: 19466
PHP applications only exist for a very short time:
That is the expected and recommended workflow of most scripting languages such as PHP. This means that every time you access a page, a new class has to be created, therefore the constructor will be called each time. So to answer your questions:
Does this have any harmful effect?
Generally, no. It's how it's supposed to work. Whether it might have a harmful effect for your application depends on exactly what you're doing in the constructor.
Can this be avoided?
This could be avoided using caching or serializing and saving the class instances, but I can't for the life come up with a good reason for why you would want this.
Any suggestions on how you would do this differently?
I wouldn't do it differently. It's how the Model-View-Controller pattern is supposed to work. Take a look at popular frameworks like Yii, CodeIgniter, CakePHP - they all do it like you.
Upvotes: 1