Home Backend Development PHP Tutorial CodeIgniter扩展核心类实例详解_PHP

CodeIgniter扩展核心类实例详解_PHP

May 28, 2016 am 11:49 AM
codeigniter Expand

本文实例讲述了CodeIgniter扩展核心类的方法。分享给大家供大家参考,具体如下:

CI中对核心类、辅助类和函数的扩展是相当方便的,配置文件中指定了subclass_prefix扩展前缀,默认为MY_,扩展时需要以该配置为前缀,下面整理下扩展方式。

1、扩展核心类

核心类位于system/core下,其中大部分类会在初始化的时候自动加载。扩展核心类的方式有两种:替换核心类和继承核心类。

替换核心类

当application/core目录下存在与system/core同名的文件时会自动替换掉核心类。以Loader.php为例,当创建application/core/Loader.php后会自动加载该类,由于该类为系统核心类,所以,如果Loader.php未实现CI_Loader类中的方法则会报错,如:

class CI_Loader
{
  ...
}

Copy after login

替换核心类需要重写其中的所有方法,以免影响核心功能。但大部分时候并不需要重写整个核心,基本上只是增加某些方法,这个时候可以采取继承的方式。

继承核心类

继承核心类需要以subclass_prefix为前缀,如扩展Input类,需创建application/core/MY_Input.php,并且MY_Input需要继承CI_Input类,如:

<&#63;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 */

Copy after login

2、扩展CI类库

system/libraries下实现了一些辅助类,当有需要扩展这些类时,和核心类的处理方式是一样的,只不过目录变成了application/libraries

3、扩展辅助函数

辅助函数存放于application/helpers目录下,辅助函数的“继承”方式与上面相同。因为CI的辅助函数都有使用function_exists来判断是否存在,所以也可以达到“重写”的目的。如在array中新增一个数组排序方法:

<&#63;php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 对二维数组进行排序
 * 
 * @param array $data 需要排序的字段
 * @param array $sort_field 按哪个键进行排序,如果不是所有键中都含有该字段则返回原数组
 * @param array $sort_type 排序方式 SORT_ASC 升序 SORT_DESC 降序
 * @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 */

Copy after login

总的来说,可以对CI框架system目录下的大部分内容进行重写,灵活度很高,扩展也很方便。但有时候也需要注意一下,并不是扩展的越多就越好,确保CI实现不了的功能再去扩展。最后既然CI提供了扩展的功能,就不要直接去修改system下的内容了。

更多关于codeigniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》和《CI(CodeIgniter)框架进阶教程》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

How to use the Aurora Push extension to implement batch message push function in PHP applications How to use the Aurora Push extension to implement batch message push function in PHP applications Jul 25, 2023 pm 08:07 PM

How to use the Aurora Push extension to implement batch message push function in PHP applications. In the development of mobile applications, message push is a very important function. Jiguang Push is a commonly used message push service that provides rich functions and interfaces. This article will introduce how to use the Aurora Push extension to implement batch message push functionality in PHP applications. Step 1: Register a Jiguang Push account and obtain an API key. First, we need to register on the Jiguang Push official website (https://www.jiguang.cn/push)

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Jun 27, 2023 pm 02:49 PM

With the development of mobile Internet, instant messaging has become more and more important and popular. For many companies, live chat is more like a communication service, providing a convenient communication method that can quickly and effectively solve business problems. Based on this, this article will introduce how to use the PHP framework CodeIgniter to develop a real-time chat application. Understand the CodeIgniter framework CodeIgniter is a lightweight PHP framework that provides a series of simple tools and libraries to help developers quickly

See all articles