blob: d322f56ebaa8a3c08dec95841754482f6a9b72f8 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Creating Libraries
3##################
4
5When we use the term "Libraries" we are normally referring to the
6classes that are located in the libraries directory and described in the
7Class Reference of this user guide. In this case, however, we will
8instead describe how you can create your own libraries within your
9application/libraries directory in order to maintain separation between
10your local resources and the global framework resources.
11
12As an added bonus, CodeIgniter permits your libraries to extend native
13classes if you simply need to add some functionality to an existing
14library. Or you can even replace native libraries just by placing
15identically named versions in your application/libraries folder.
16
17In summary:
18
19- You can create entirely new libraries.
20- You can extend native libraries.
21- You can replace native libraries.
22
23The page below explains these three concepts in detail.
24
25.. note:: The Database classes can not be extended or replaced with your
26 own classes. All other classes are able to be replaced/extended.
27
28Storage
29=======
30
31Your library classes should be placed within your application/libraries
32folder, as this is where CodeIgniter will look for them when they are
33initialized.
34
35Naming Conventions
36==================
37
38- File names must be capitalized. For example: Myclass.php
39- Class declarations must be capitalized. For example: class Myclass
40- Class names and file names must match.
41
42The Class File
43==============
44
45Classes should have this basic prototype (Note: We are using the name
46Someclass purely as an example)::
47
48 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Someclass {     public function some_function()     {     } } /* End of file Someclass.php */
49
50Using Your Class
51================
52
53From within any of your :doc:`Controller <controllers>` functions you
54can initialize your class using the standard::
55
56 $this->load->library('someclass');
57
58Where *someclass* is the file name, without the ".php" file extension.
59You can submit the file name capitalized or lower case. CodeIgniter
60doesn't care.
61
62Once loaded you can access your class using the lower case version::
63
64 $this->someclass->some_function();  // Object instances will always be lower case
65
66Passing Parameters When Initializing Your Class
67===============================================
68
69In the library loading function you can dynamically pass data as an
70array via the second parameter and it will be passed to your class
71constructor::
72
73 $params = array('type' => 'large', 'color' => 'red'); $this->load->library('Someclass', $params);
74
75If you use this feature you must set up your class constructor to expect
76data::
77
78 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Someclass {     public function __construct($params)     {         // Do something with $params     } } ?>
79
80You can also pass parameters stored in a config file. Simply create a
81config file named identically to the class file name and store it in
82your application/config/ folder. Note that if you dynamically pass
83parameters as described above, the config file option will not be
84available.
85
86Utilizing CodeIgniter Resources within Your Library
87===================================================
88
89To access CodeIgniter's native resources within your library use the
90get_instance() function. This function returns the CodeIgniter super
91object.
92
93Normally from within your controller functions you will call any of the
94available CodeIgniter functions using the $this construct::
95
96 $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); etc.
97
98$this, however, only works directly within your controllers, your
99models, or your views. If you would like to use CodeIgniter's classes
100from within your own custom classes you can do so as follows:
101
102First, assign the CodeIgniter object to a variable::
103
104 $CI =& get_instance();
105
106Once you've assigned the object to a variable, you'll use that variable
107*instead* of $this::
108
109 $CI =& get_instance(); $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.
110
111.. note:: You'll notice that the above get_instance() function is being
112 passed by reference::
113
114 $CI =& get_instance();
115
116 This is very important. Assigning by reference allows you to use the
117 original CodeIgniter object rather than creating a copy of it.
118
119Replacing Native Libraries with Your Versions
120=============================================
121
122Simply by naming your class files identically to a native library will
123cause CodeIgniter to use it instead of the native one. To use this
124feature you must name the file and the class declaration exactly the
125same as the native library. For example, to replace the native Email
126library you'll create a file named application/libraries/Email.php, and
127declare your class with::
128
129 class CI_Email { }
130
131Note that most native classes are prefixed with CI\_.
132
133To load your library you'll see the standard loading function::
134
135 $this->load->library('email');
136
137.. note:: At this time the Database classes can not be replaced with
138 your own versions.
139
140Extending Native Libraries
141==========================
142
143If all you need to do is add some functionality to an existing library -
144perhaps add a function or two - then it's overkill to replace the entire
145library with your version. In this case it's better to simply extend the
146class. Extending a class is nearly identical to replacing a class with a
147couple exceptions:
148
149- The class declaration must extend the parent class.
150- Your new class name and filename must be prefixed with MY\_ (this
151 item is configurable. See below.).
152
153For example, to extend the native Email class you'll create a file named
154application/libraries/MY_Email.php, and declare your class with::
155
156 class MY_Email extends CI_Email { }
157
158Note: If you need to use a constructor in your class make sure you
159extend the parent constructor::
160
161 class MY_Email extends CI_Email {     public function __construct()     {         parent::__construct();     } }
162
163Loading Your Sub-class
164----------------------
165
166To load your sub-class you'll use the standard syntax normally used. DO
167NOT include your prefix. For example, to load the example above, which
168extends the Email class, you will use::
169
170 $this->load->library('email');
171
172Once loaded you will use the class variable as you normally would for
173the class you are extending. In the case of the email class all calls
174will use::
175
176 $this->email->some_function();
177
178Setting Your Own Prefix
179-----------------------
180
181To set your own sub-class prefix, open your
182application/config/config.php file and look for this item::
183
184 $config['subclass_prefix'] = 'MY_';
185
186Please note that all native CodeIgniter libraries are prefixed with CI\_
187so DO NOT use that as your prefix.