嵌套扩展控制器中的Codeigniter调用函数

时间:2018-12-12 03:10:22

标签: php codeigniter controller

我有一个MY_Controller类的MY_Controller php文件,并且在我的Other_Controller文件夹中扩展了My_Controller类的application/core

class MY_Controller extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
    }
    function SomeMethod()
    {
        echo "method MY_Controller";
    }
}
class Other_Controller extends My_Controller 
{
    function __construct()
    {
       parent::__construct();
    }
}

在我的Application/controller文件夹中:

    Class Main extends Other_Controller
    { include(APPPATH.'core/Other_Controller.php');
        function __construct()
        {
            parent::__construct();
            // Call SomeMethod function name?
        }
    }

我可以从MY_Controller到主控制器调用SomeMethod函数吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以像在构造函数中一样使用parent关键字:

class Main extends Other_Controller
{
    function __construct()
    {
        parent::__construct();
        parent::SomeMethod(); // echoes "method MY_Controller"
    }
}

Other_Controller类覆盖MY_Controller::SomeMethod的情况下,您仍然可以使用完整的类名和scope resolution operator ::SomeMethod类中调用原始的Main:< / p>

class MY_Controller extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
    }

    function SomeMethod()
    {
        echo "method MY_Controller";
    }
}

class Other_Controller extends My_Controller 
{
    function __construct()
    {
       parent::__construct();
    }

    function SomeMethod()
    {
        echo "method Other_Controller";
    }
}

class Main extends Other_Controller
{
    function __construct()
    {
        parent::__construct();
        parent::SomeMethod(); // echoes "method Other_Controller"
        MY_Controller::SomeMethod(); // echoes "method MY_Controller"
    }
}