CI framework--Creating a class library

Source: Internet
Author: User

When we use the word "class library", we usually refer to those classes that are located in the Libraries directory.

Next, we'll show you how to create your own class library in the application/libraries directory, independent of the Global Framework class library.

Also, if you want to add some extra functionality to an existing class library, CodeIgniter allows you to extend the native class, or you can even place a file with the same name as the native class library in your application/libraries directory to completely replace it.

To sum up:

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

Note: Other classes are available except that the database class cannot be extended or replaced by your class.

Storage location

Your class library files should be placed in the application/libraries directory, and CodeIgniter will look for these classes in this directory when you initialize the class.

Naming conventions
    • The first letter of the file name must be capitalized, for example: myclass.php
    • The first letter of the class name definition must be capitalized, for example: Class Myclass
    • The class name and file name must be identical
class file

The class should be defined as the following prototype:

<? PHP defined Exit (' No Direct script access allowed '); class SomeClass {    publicfunction  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 and does not include the. php file name extension. Filenames can be written in uppercase letters, or in all lowercase, and codeigniter can be recognized.

Once loaded, you can use lowercase names to access your classes:

$this->someclass->some_method ();
Incoming parameters when initializing a class

When loading a 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 $params);

If you use this feature, you must add parameters when defining the class's constructor:

defined Exit (' No Direct script access allowed '); class SomeClass {    publicfunction __construct ($params)    {         // Do something with $params     }}

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

Using CodeIgniter resources in your class library

Use the get_instance () function in your class library to access the native resource of CodeIgniter, which returns the CodeIgniter Super Object .

Typically, in your controller method you will use the $this to invoke all available CodeIgniter methods :

$this->load->helper (' url '); $this->load->library (' Session '); $this->config->item (' Base_url ');

but $this can only be used directly in your controller, model, or view , if you want to use the CodeIgniter class in your own class, you can do the following:

First, assign the CodeIgniter object to a variable:

$CI =& get_instance ();

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

$CI =& get_instance (); $CI->load->helper (' url '); $CI->load->library (' Session '); $CI->config->item (' Base_url ');

Note:

The get_instance () function above is passed by reference:

$CI =& get_instance();

It is very important that the reference assignment allows you to use the original CodeIgniter object instead of creating a copy.

Since the class library is a class, it is best to use the OOP principle fully, so that in order for all methods in the class to use the CodeIgniter Super object, it is recommended to assign it to a property:

classExample_library {protected $CI; //We'll use a constructor, as can ' t directly call a function//from a property definition.     Public function__construct () {//Assign the CodeIgniter super-object         $this->ci =& get_instance (); }     Public functionfoo () {$this->ci->load->helper (' url ');    redirect (); }     Public functionBar () {Echo $this->ci->config->item (' Base_url '); }}

Replace the native class library with your own class library

Simply change your class file name to match 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 e-mail class, you want to create a new application/libraries/email.php file, Then define your class as defined:

class Ci_email {}

Note Most native classes start with Ci_.

To load your class library, as in the standard method:

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

Note:

Note that the database class cannot be replaced by your own class.

Extending native class libraries

If you just want to add some functionality to an existing class library, such as adding one or two methods, it feels a little overkill to replace the whole class. In this case, the best approach is to extend the class library. Extending a class is similar to replacing a class, except for the following points:

    • Classes must inherit from the parent class when they are 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 then define your class:

class extends Ci_email {}

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

class extends Ci_email {    publicfunction __construct ($configarray())    {        parent:: __construct ($config);    }}

Note:

Not all class library constructors have the same parameters, so let's look at how it is implemented before you extend 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 the Email class you extended in the previous example, you can use:

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

Once loaded, you still use class variables as usual to access your extended classes, as an example of an email class, to access it in the following ways:

$this->email->some_method ();

Setting a custom prefix

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

$config [' subclass_prefix '] = ' my_ ';

Note: All original CodeIgniter class libraries start with ci_ , so please do not use this as your custom prefix.

CI framework--Creating a class library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.