3

我正在尝试学习 OOP,只是为了测试,我想用不同的方法创建一个主类。在这种情况下,我想“加载”一个像 CodeIgniter 这样的控制器:

$app->load->controller('test')

load()是一种方法,Main()但是呢controller()

这是我到目前为止所拥有的,但显然它不起作用。我不明白如何将控制器名称传递给负载/控制器类

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');
4

2 回答 2

4

你可以试试这个

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

演示。

于 2013-05-27T21:06:03.027 回答
0
class Main 
{
    public function load()
    {
        $loader = new Load();
        $loader->controller('index');
    }
}
class Load 
{
    public function controller($class)
    {
        $test=new Test();
        $test->index();    
    }
}
class Test 
{
    function index()
    {
        echo "Hello";
    }
}
$main = new Main();
$main->load('test');
于 2014-08-09T12:31:55.133 回答