Home > php教程 > PHP开发 > body text

CodeIgniter study notes Item9--Class library in CI

黄舟
Release: 2016-12-29 10:34:18
Original
1193 people have browsed it

CodeIgniter Class Library

All class library files are stored in the system/libraries folder. In most cases you need to initialize them in the controller before you can use them:

[code]$this->load->library('class name');
Copy after login

class name is the class name you want to use. For example, to load the "form validation class", you can do this:

[code]$this->load->library('form_validation');
Copy after login

Create your class library file

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

Naming convention

The first letter of the file name is capitalized. For example: Myclass.php

The first letter of the class declaration is capitalized. For example: class Myclass

The name of the class and the file name should be the same.

All classes should have a basic prototype

[code]<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
class Someclass {
    public function some_function()
    {
    }
}
/* End of file Someclass.php */
Copy after login

In all Controller functions, you can initialize your class in the following standard way:

[code]$this->load->library(&#39;someclass&#39;);
Copy after login

When someclass is the file name, there is no need to add the ".php" extension. The name here is not case-sensitive.

Once your custom class is loaded, you can pass Call the class in the following way, remember to use lowercase names:

[code]$this->someclass->some_function();  // 对象的实例名永远都是小写的
Copy after login

When initializing the class library, you can dynamically pass the array to the class constructor through the second parameter:

[code]$params = array(&#39;type&#39; => &#39;large&#39;, &#39;color&#39; => &#39;red&#39;);
$this->load->library(&#39;Someclass&#39;, $params);
Copy after login

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

[code]<?php if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
class Someclass {
    public function __construct($params)
    {
        // Do something with $params
    }
}
?>
Copy after login


To be used in your own To access the original resources of CodeIgniter in a defined class library, you must use the get_instance() function. Generally speaking in your controller function you can call any available CodeIgniter function through $this:

[code]$this->load->helper(&#39;url&#39;);
$this->load->library(&#39;session&#39;);
$this->config->item(&#39;base_url&#39;);
Copy after login

When you want to use the CodeIgniter original class in a custom class, you can do this :

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

[code]$CI =& get_instance();
Copy after login

Once you define an object as a variable, you can use that variable name instead of $this:

[code]$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
Copy after login

Replace the original class

Simply name your own class the same as the original class to make CodeIgniter use the new class. To use this feature, 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 have to create a file application/libraries/Email.php, and declare the classes as follows:

[code]class CI_Email 
{
}
Copy after login

Extend existing classes

If you need to To add one or two new features to the library, there is no need to replace the entire class library file. You can simply extend (inherit) the existing class. Extending a class is like adding some exceptions to the class:

The extended class must be declared to be extended from the parent class.

The file containing the newly extended class must be prefixed with MY_ (this option is configurable).

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

[code]class MY_Email extends CI_Email 
{
}
Copy after login


To load an extension subclass, you should use standard character names, please do not use prefixes. For example, to load the email extension subclass mentioned above, you should write:

[code]$this->load->library(&#39;email&#39;);
Copy after login


The above is the content of CodeIgniter study notes Item9--the class library in CI. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template