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?