본문 바로가기
  • AI (Artificial Intelligence)
Programming/PHP

Inheritance between controllers in Codeigniter

by 로샤스 2017. 7. 21.

Today, we will talk about the inheritance between controllers in CodeIgniter. At first glance, it could seem very simple to do and it should not be more complicated than this:

class Parent_controller extends Controller {

    function __construct()
    {
       parent::Controller();
       ...
    }
    ...
}

class Child_controller extends Parent_controller {
    function __construct()
    {
        parent::__construct();
        ...
    }
    ...
}

If we run this code as it is, CodeIgniter will return an error saying that it can not find Parent_controller class, which I founded quite surprising.In order to make possible that the controller which inherits the methods can use its parent methods, and thus, to make effective the inheritance, we have two ways.

Method 1

We can do it in the traditional way of PHP, that is, explicitly including the files that contain the classes from our class inherit. Therefore, in the file child_controller.php we include the following code:

require_once APPPATH.'controllers/parent_controller.php';
    class Child_controller extends Parent_controller {
...
}

This way is perfectly valid and it will work without any problem. In fact, if the server where we run our code has a PHP4, this is the only way we can do this. Since today it is pretty weird to find PHP4 on a web server, this form does not seem to be the smartest or the most comfortable, since we have to replicate a lot of code writing all “requires” in each of the files where we want to use inheritance. If we are programming for PHP5, the best option is to use the __autoload() function. So that, we are going to use it.

Method 2: __autoload() function

As we said before, this function is one of the many benefits of PHP5 respect its previous versions. To spare us the tiresome task of having to invoke each and every one of the classes files which inherits another, we can define our function __autoload() and thus, it is the system that is responsible for automatically checking if a file has not been included yet, in which case it adds the file to the namespace. Calling this function is like to give a last chance to the system to include a class file before launching the error.
It is remarkable that this function will be useful as long as both controllers, parent and child, are in the same directory. If our structure is like: controllers / parent.php and controllers / subfolder / child.php, then this method will not be valid. We should include the subfolder into the autoload in order to check if our child controller is inside it.
In order to use this function in CodeIgniter we just need to include the following code (for instance) at the end of your configuration file system / application / config / config.php:

function __autoload($class)
{
    if (file_exists(APPPATH."controllers/".$class.EXT))
    {
        require_once(APPPATH.'controllers/'.$class.EXT);
    }
}

Thus PHP will try to call this function before throwing an error because it could not find the class file which is needed to carry out the inheritance. With this we have ready the inheritance between classes in CodeIgniter with all levels we needed.




한글로 간결하게 정리하면,

require_once APPPATH.'controllers/parent_controller.php';
    class Child_controller extends Parent_controller {
...
}

이걸 쓰세요..




출처 : https://www.agustinvillalba.com/inheritance-between-controllers-codeigniter/















댓글