CI Framework Realizes the Method of Creating Custom Class Library

  • 2021-11-10 09:10:02
  • OfStack

In this paper, an example is given to describe the method of creating a custom class library in CI framework. Share it for your reference, as follows:

When we use the word "class library", we usually mean those classes that are located in the directory libraries.

Next we'll show you how to create your own class library in the application/libraries directory, separate from the global framework class library.

In addition, CodeIgniter allows you to extend the native class if you want to add some additional functionality to the existing class library, or you can even replace it entirely by placing a file with the same name as the native class library in your application/libraries directory.

To sum up:

You can create a whole new class library, You can extend the native class library, You can replace the native class library.

Note: All classes except the database class can not be extended or replaced by your class.

Storage location

Your class library files should be placed in the application/libraries directory where CodeIgniter will look for classes when you initialize them.

Naming convention

Filename initials must be uppercase, for example: Myclass. php Class name definition must be capitalized, for example: class Myclass Class name and file name must be 1

Class file

Class should be defined as the following prototype:


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
 public function some_method()
 {
 }
}

Use your class

Initialize your class with the following code in any method of your controller:


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

Where someclass is the file name, excluding the. php file extension. File names can be written in uppercase or all lowercase, and can be recognized by CodeIgniter.

1 Once loaded, you can use lowercase letter names to access your class:


$this->someclass->some_method();

Pass in parameters when initializing a class

When loading the class library, you can dynamically pass an array of data through the second parameter, which will be passed to the constructor of your class:


$params = array('type' => 'large', 'color' => 'red');
$this->load->library('someclass', $params);

If you use this function, you must add parameters when defining the class constructor:


<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
 public function __construct($params)
 {
  // Do something with $params
 }
}

You can also pass the parameters in a configuration file by simply creating a configuration file with the same name as the class file and saving it to your application/config/directory. Note that if you use the method described above to pass parameters dynamically, the configuration file will not be available.

Use CodeIgniter resources in your class library

Use in your class library get_instance() Function to access the native resources of CodeIgniter, which returns the CodeIgniter super object.

Usually, in your controller method, you will use $this To call all available CodeIgniter methods:


$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');

But $this It can only be used directly in your controller, model or view. If you want to use CodeIgniter class in your own class, you can do this as follows:

First, assign the CodeIgniter object to a variable:


$CI =& get_instance();

1 Once you assign an CodeIgniter object to a variable, you can use this variable instead of $this


$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

Note:

Above get_instance() Function is passed by reference:


$CI =& get_instance();

This is important because reference assignment allows you to use the original CodeIgniter object instead of creating a copy.

Since the class library is a class, we'd better make full use of the OOP principle. Therefore, in order for all methods in the class to use the CodeIgniter super object, it is recommended to assign it to a property:


class Example_library {
 protected $CI;
 // We'll use a constructor, as you can't directly call a function
 // from a property definition.
 public function __construct()
 {
  // Assign the CodeIgniter super-object
  $this->CI =& get_instance();
 }
 public function foo()
 {
  $this->CI->load->helper('url');
  redirect();
 }
 public function bar()
 {
  echo $this->CI->config->item('base_url');
 }
}

Replace the native class library with your own

Simply change the name of your class file to the same as the native class library file, and CodeIgniter will use it to replace the native class library. To use this feature, you must change your class library file and class definition to exactly the same as the native class library. For example, to replace the native Email class, you need to create a new application/libraries/Email. php file, and then define your class:


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

0

Note that most native classes begin with CI_.

To load your class library, like standard method 1:


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

1

Note:

Note that database classes cannot be replaced by your own classes.

Extend native class library

If you just want to add 1 functionality to an existing class library, such as adding 1 or 2 methods, replacing the whole class feels like killing the chicken. In this case, the best way is to extend the class library. Extending a class is similar to replacing a class, except for the following points:

A class must inherit from its parent class when defined. Your new class name and file name must be prefixed with MY_ (this is configurable, see below)

For example, to extend the native Email class, you need to create a new file named application/libraries/MY_Email. php and define your class:


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

2

If you need to use a constructor in your class, make sure you call the constructor of the parent class:


class MY_Email extends CI_Email {
 public function __construct($config = array())
 {
  parent::__construct($config);
 }
}

Note:

Not all class library constructors have 1-like arguments, so let's look at how it is implemented before extending the class library.

Load your extension class

To load your extension class, use the same syntax as usual. Do not include prefixes. For example, to load your extended Email class in the above example, you can use:


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

4

Once loaded, you still use class variables to access your extended class as usual. Take email class as an example, and access it by the following methods:


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

5

Setting custom prefixes

To prefix your own class, you can open the file application/config/config. php and find the following:


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

6

Note: All the original CodeIgniter class libraries begin with CI_, so do not use this as your custom prefix.

More readers interested in CodeIgniter can check out the topics on this site: "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "php Excellent Development Framework Summary", "ThinkPHP Introduction Tutorial", "ThinkPHP Common Methods Summary", "Zend FrameWork Framework Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php + mysql Database Operation Introduction Tutorial" and "php Common Database Operation Skills Summary"

I hope this article is helpful to PHP programming based on CodeIgniter framework.


Related articles: