I'm trying to learn OOP and just for testing I want to create a main class with different methods. In this case, I want to "load" a controller like CodeIgniter:
$app->load->controller('test')
load()
is a method of Main()
but what about controller()
?
This is what I have so far, but obviously it is not working. I can't understand how to pass the controller name to the Load/controller class
class Main {
public function load()
{
$loader = new Load();
$loader->controller('index');
}
}
class Load {
public function controller($class)
{
$class = ucfirst($class);
$class = new $class();
$class->index();
}
}
class Test {
function index()
{
echo 'class: test - method: index';
}
}
$main = new Main;
$controller = $main->load->controller('test');
You may try this
class Main {
public function load()
{
//include "Load.php";
return new Load();
}
}
class Load {
public function controller($class = '')
{
if( file_exists( $class . '.php') ) {
include $class . '.php';
$class = ucfirst($class);
return new $class;
}
}
}
$main = new Main;
$controller = $main->load()->controller('testClass');
$controller->index(); // Assume testClass has an index method