Detailed description of the extended core class instances of CodeIgniter

Source: Internet
Author: User
This article mainly introduces the extended core class of CodeIgniter, and analyzes the extension methods and implementation skills of the extended CI class library and auxiliary functions of CodeIgniter based on examples, for more information about how to extend the core class of CodeIgniter, see the example in this article. We will share this with you for your reference. The details are as follows:

The extension of core classes, auxiliary classes, and functions in CI is quite convenient. the prefix of subclass_prefix is specified in the configuration file. the default prefix is MY _. you need to use this prefix for extension, the following describes the extension methods.

1. Extended core class

The core class is located under system/core, and most of the classes are automatically loaded during initialization. There are two ways to expand the core class: replacing the core class and inheriting the core class.

Replace core classes

When a file with the same name as system/core exists in the application/core directory, the core class is automatically replaced. To Loader. for example, when you create application/core/Loader. php will automatically load this class. because this class is the system core class, if Loader. if php does not implement methods in the CI_Loader class, an error is reported, for example:

class CI_Loader{  ...}

To replace the core class, you must override all the methods to avoid affecting core functions. However, in most cases, you do not need to overwrite the entire core. basically, you only need to add some methods. in this case, you can use the inheritance method.

Inherit core classes

The inheritance core class must be prefixed with subclass_prefix. to expand the Input class, you must create application/core/MY_Input.php and MY_Input must inherit the CI_Input class, for example:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class MY_Input extends CI_Input{  function _clean_input_keys($str)  {    $config = &get_config('config');    if ( ! preg_match("/^[".$config['permitted_uri_chars']."]+$/i", rawurlencode($str))) {      exit('Disallowed Key Characters.');    }    // Clean UTF-8 if supported    if (UTF8_ENABLED === TRUE) {      $str = $this->uni->clean_string($str);    }    return $str;  }}/* End of file MY_Input.php *//* Location: ./application/core/MY_Input.php */

2. extend the CI class library

System/libraries implements some helper classes. when these classes need to be extended, they are processed in the same way as the core classes, except that the directory is changed to application/libraries.

3. extended auxiliary functions

Auxiliary functions are stored in the application/helpers directory, and the "inheritance" method of auxiliary functions is the same as above. Because all CI auxiliary functions use function_exists to determine whether the function exists, the function can be rewritten. For example, add an array sorting method in array:

<? Php if (! Defined ('basepath') exit ('no direct script access allowed '); /*** sort two-dimensional arrays ** @ param array $ field to be sorted by data * @ param array $ sort_field, if not all keys contain this field, return the original array * @ param array $ sort_type sort method SORT_ASC ascending SORT_DESC descending order * @ return array */function array_field_sort ($ data, $ sort_field, $ sort_type = SORT_ASC) {if (! Is_array ($ data) {return false;} $ sort_arr = array (); foreach ($ data as $ key => $ val) {if (isset ($ val [$ sort_field]) {$ sort_arr [$ key] = $ val [$ sort_field] ;}} if (count ($ sort_arr) = count ($ data) {array_multisort ($ sort_arr, $ sort_type, $ data);} return $ data;}/* End of file MY_array_helper.php * // * Location :. /application/helpers/MY_array_helper.php */

In general, you can rewrite most of the content in the CI framework system directory, which is flexible and easy to expand. But sometimes you need to pay attention that the more expansion, the better, and ensure that CI functions cannot be implemented before expansion. Finally, since CI provides the extended function, do not directly modify the content in the system.

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.