假設我有兩個MY_Controller的子控制器,比如Child_1 & Child_2.In MY_Controller我有兩個方法method_1 & method_2作爲在codeigniter中如何從子控制器調用該方法時從父控制器中的方法獲取對子控制器的引用
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
some code ...
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
我從child_1 & child_2使用電話method_2()
class Child_1 extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function method_1(){//custom implementation goes here}
public function some_method()
{
...some code
$this->method_2();//call inherited method method_2()
}
}
爲child_2
此代碼類似的代碼是正確的,你可以使用$這 - > method_2()調用調用父類的方法。但是,爲什麼在父類中調用孩子的方法,這與繼承是相反的。 – aeonsleo