在codeigniter中,当从子控制器调用该方法时,如何从父控制器中的方法获取对子控制器的引用

时间:2016-04-20 18:49:44

标签: php codeigniter

假设我有两个MY_Controller的子控制器,比如说Child_1& Child_2。在MY_Controller中我有两种方法method_1& method_2 as

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&中调用 method_2() child_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的类似代码

1 个答案:

答案 0 :(得分:0)

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 ...

    $this->method_1(); //Call the method directly it will implement the child's method
    /*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
    }
}