CodeIgniter extended Core class examples CodeIgniter detailed _php tutorials

Source: Internet
Author: User
Tags codeigniter

CodeIgniter extended Core class examples, CodeIgniter detailed


The examples in this paper describe the methods of CodeIgniter extension core classes. Share to everyone for your reference, as follows:

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

1. Extended Core class

The core class is located under System/core, where most of the classes are loaded automatically 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 is present in the Application/core directory. Take loader.php as an example, the class is automatically loaded when the application/core/loader.php is created, because the class is a system core class, so if loader.php does not implement methods in the Ci_loader class, it will report an error, such as:

Class ci_loader{...  }

Replacing a core class requires overriding all of its methods to avoid affecting 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

The inheriting core class needs to be prefixed with subclass_prefix, such as extending the Input class, creating application/core/my_input.php, and my_input need to inherit the Ci_input class, 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

System/libraries implementation of some auxiliary classes, when it is necessary to extend these classes, and the core class processing is the same, but the directory becomes the Application/libraries

3. Extended Auxiliary functions

The auxiliary functions are stored in the Application/helpers directory, and the auxiliary functions are "inherited" in the same way as above. Because CI auxiliary functions have to use function_exists to determine whether there is, so can also achieve the purpose of "rewrite". For example, add an array ordering method in array:

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed ');/** * Sort two-dimensional arrays *  * @param array $data required Sorted fields * @param array $sort _field by which key to sort, if not all keys contain the field then return the original array * @param array $sort _type sort sort_asc Ascending sort_desc Descending * @ 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, most of the content in the CI Framework system directory can be rewritten with high flexibility and ease of expansion. But there are times when it's important to note that the more you expand, the better it is to make sure that the features that CI can't implement are expanded. Finally, since CI provides extended functionality, do not directly modify the contents of the system.

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial" and "CI (codeigniter) Framework Advanced Tutorial"

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

Articles you may be interested in:

    • Case analysis of controller succession problem in CodeIgniter controllers
    • 2 codeigniter file Batch Upload Controller example
    • CodeIgniter Multi-language Implementation method detailed
    • CI (CodeIgniter) Model Usage Example analysis
    • CodeIgniter View Usage Considerations
    • CodeIgniter Read and write separation implementation method detailed
    • CI (CodeIgniter) Simple statistics access to the number of implementation methods
    • CodeIgniter Custom Controller My_controller usage Analysis

http://www.bkjia.com/PHPjc/1094768.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094768.html techarticle CodeIgniter Extended Core class examples, CodeIgniter detailed examples of this article describes the CodeIgniter extension core class methods. Share to everyone for your reference, specifically as follows: CI in the core class ...

  • 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.