**ci Creating a class library (creating your own tool classes, etc.)

Source: Internet
Author: User
Tags codeigniter

To create a class library

When we use the term "class library", we generally refer to the classes located in the libraries folder, which are discussed in the wiki's "Class Library Reference" section. In the current topic, we will discuss how to application/libraries Folders to create your own class libraries and keep them separate from the full-frame resources.

As an additional feature, when you need to simply add some functionality to the original class, CodeIgniter can make your class library self-contained extend . You can even pass in application/libraries Folder with a class library file of the same name to completely replace the original class.

Anyway:

    • You can create a completely new class library.
    • You can extend the original class library.
    • You can replace the original class library.

The following pages will cover these three concepts in depth.

Note: except that the database class cannot be extended or replaced, the rest of the classes are available.

Build your class library file

Your class library files must be saved in the application/libraries folder, and CodeIgniter will look for and initialize them in this folder.

Naming conventions
    • Capitalize the first letter of the file name. Example: myclass.php
    • class declaration is capitalized in the first letter. For example:class Myclass
    • The name and file name of the class should be the same.
class file

All classes should have a basic prototype (note, here we take Someclass this name as an example):

<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘); 

class Someclass {

    public function some_function()
    {
    }
}

/* End of file Someclass.php */

Use your own class

In all controller functions, you can initialize your class in the following standard ways:

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

When SomeClass is a file name, you do not need to add the ". php" extension. Here the names are not case-sensitive.

Once your custom class is loaded, you can invoke the class in the following way, using 小写 the name:

$this->someclass->some_function();  // 对象的实例名永远都是小写的

Passing parameters when initializing a custom class

When initializing a class library, you can dynamically pass the array to the constructor of the class with the second argument:

$params = array(‘type‘ => ‘large‘, ‘color‘ => ‘red‘);

$this->load->library(‘Someclass‘, $params);

When you use this feature, you must add parameters to the constructor of the class:

<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);

class Someclass {

    public function __construct($params)
    {
        // Do something with $params
    }
}

?>

You can also pass parameters that are stored in the configuration file. You simply create a 类文件名 config file with the same and save it in the application/config/ folder. Note When you dynamically pass parameters in the way described above, The options in the config file will not work.

Initialize the CodeIgniter resource in your custom class library

To access CodeIgniter's original resources in your custom class library, you must use get_instance() a function. This function returns a CodeIgniter super object.

In general, you can $this call any of the available CodeIgniter functions in your controller function:

$this->load->helper(‘url‘);
$this->load->library(‘session‘);
$this->config->item(‘base_url‘);
//etc.

$this, only directly in your own controllers, models and views. When you want to use the CodeIgniter primitive class in your custom class, you can do this:

First, define the CodeIgniter object to assign to a variable:

$CI =& get_instance();

Once you define an object as a variable, you can replace it with that variable name $this :

$CI =& get_instance();

$CI->load->helper(‘url‘);
$CI->load->library(‘session‘);
$CI->config->item(‘base_url‘);
//etc.

Note: you will notice that the get_instance () function is passed in a referenced manner:

$CI =& get_instance();

这十分重要.Assigning a variable by reference uses the original CodeIgniter object instead of creating a copy.

Replace the original class with your own class

Simply name your own class like the original class to make CodeIgniter use this new class. To use this attribute, the file name and class declaration must be exactly the same as the original class. For example, to replace the original Email class library. You must create a file application/libraries/email.phpand declare the class as follows:

class CI_Email {

}

Note Most of the original classes CI_ are prefixed.

You can load your own class with just the standard load function:

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

Note: database cannot be replaced by your custom class at this time.

Extending existing Classes

If you need to add one or two new features to an existing class library, it's completely unnecessary to replace the entire class library file. You simply extend (inherit) the current class, extending a class as if adding some exceptions to the class:

    • Extended classes must be declared to be extended by the parent class.
    • The file in which the newly extended class resides must be MY_ prefixed (this option is configurable, as described below).

For example, to extend the original class Email class you want to create the file application/libraries/ MY_Email.php and declare it in the file as follows:

class MY_Email extends CI_Email {

}

Note: If you need to use constructors in a class, you must explicitly inherit the parent class constructor in the constructor:

class MY_Email extends CI_Email {

    public function __construct()
    {
        parent::__construct();
    }
}

Load your sub-class

To load the extension subclass, you should use the standard character name. Please do not use prefixes. For example, to load an e-mail extension subclass As mentioned above, you should write:

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

Once the extension subclasses are loaded, they can be used like normal classes. All functions in the e-mail class can be called:

$this->email->some_function();

Setting a custom prefix

To set your own subclass prefix, open the application/config/config.php file and find this:

$config[‘subclass_prefix‘] = ‘MY_‘;

Note that all the original CodeIgniter class libraries are CI_ prefixed, so do not use ci_ as your own prefix.

**ci Creating a class library (creating your own tool classes, etc.)

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.