CodeIgniter extended Core Class example detailed _php example

Source: Internet
Author: User
Tags array sort codeigniter

This article illustrates the method of CodeIgniter extending core classes. Share to everyone for your reference, specific as follows:

The extension of core classes, auxiliary classes, and functions in CI is quite handy, the subclass_prefix extension prefix is specified in the configuration file, the default is My_, and the extension needs to be prefixed with the configuration, and the extension is sorted below.

1, expand the core category

The core classes are located under System/core, where most of the classes are automatically loaded at initialization time. There are two ways to extend a core class: replace the core class and inherit the core class.

Replace Core class

The core class is automatically replaced when a file with the same name as System/core exists under the Application/core directory. In the case of loader.php, the class is loaded automatically when the application/core/loader.php is created, because the class is the system core class, so if loader.php does not implement a method in the Ci_loader class, it will report an error, such as:

Class Ci_loader
{
  ...
}

Replacing the core class requires rewriting all of these methods so as not to affect the core functionality. But most of the time there is no need to rewrite the whole core, basically just add some methods, this time can take the way of inheritance.

Inheriting core classes

Inheriting core classes needs to be prefixed with subclass_prefix, such as extending the Input class, creating application/core/my_input.php, and My_input inheriting ci_input classes, such as:

<?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, Extended CI class library

Some helper classes are implemented under System/libraries, and when it is necessary to extend these classes, the core classes are treated the same way, except that the directory becomes application/libraries

3. Extended Auxiliary functions

Auxiliary functions are stored in the Application/helpers directory, and the auxiliary functions are "inherited" in the same way as above. Because CI's auxiliary function has the use function_exists to judge whether exists, therefore also may achieve "the rewrite" the goal. For example, add an array sort method in the array:

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed ');
/**
 * To sort two-dimensional arrays * 
 @param array $data fields to sort by
 @param array $sort _field The key to sort, or return the original array if not all of the keys contain the field
 * @param array $sort _type sort sort_asc Ascending sort_desc descending
 * @return array
/function Array_field_sort ($da TA, $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, it is possible to rewrite most of the content under the CI Framework System directory, with a high degree of flexibility and ease of expansion. But sometimes also need to pay attention to, not the more expansion of the better, to ensure that CI can not achieve the function to expand. Finally, since CI provides extended functionality, do not directly modify the content under System.

More readers interested in CodeIgniter-related content can view the site topics: "CodeIgniter Introductory Course" and "CI (CodeIgniter) Framework Advanced Course"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

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.