How do you add abstract class library in the Codeigniter framework?

前端 未结 5 633
天命终不由人
天命终不由人 2020-12-31 20:27

I have the following code in file called AbstractClass.php in the libraries folder

abstract class AbstractClass {
  abstract prote         


        
5条回答
  •  孤独总比滥情好
    2020-12-31 21:02

    I use abstract classes with CodeIgniter libraries because I have common methods which I want all inherited classes to use which are meaningless on their own. I don't know if what I'm about to suggest is best practise. I suspect it's not, but I personally find it useful. Here's how I do it:

    1. Create a new classes folder in the CodeIgniter application folder.

    2. Add this folder to the path. (I usually do this in the controller.)

      if (!strstr(get_include_path(), APPPATH . 'classes')) {
          ini_set('include_path', get_include_path() . ':' . APPPATH . 'classes');
      }
      
    3. Create the abstract classes, or other classes, in the classes folder.

    4. Create an extended CodeIgniter library:

      require_once('an_abstract_class.php');
      class concrete_library extends an_abstract_class {
      
    5. Use the library as normal:

      $this->load->library('concrete_library');
      

    That should do the trick. I hope this is useful.

提交回复
热议问题