我有一个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函数吗?
答案 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"
}
}