Remove file execution permission.
[living-lab-site.git] / user_guide / general / creating_libraries.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3 <head>
4
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>Creating Libraries : CodeIgniter User Guide</title>
7
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11 <script type="text/javascript" src="../nav/nav.js"></script>
12 <script type="text/javascript" src="../nav/prototype.lite.js"></script>
13 <script type="text/javascript" src="../nav/moo.fx.js"></script>
14 <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16 <meta http-equiv='expires' content='-1' />
17 <meta http-equiv= 'pragma' content='no-cache' />
18 <meta name='robots' content='all' />
19 <meta name='author' content='ExpressionEngine Dev Team' />
20 <meta name='description' content='CodeIgniter User Guide' />
21
22 </head>
23 <body>
24
25 <!-- START NAVIGATION -->
26 <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27 <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28 <div id="masthead">
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30 <tr>
31 <td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33 </tr>
34 </table>
35 </div>
36 <!-- END NAVIGATION -->
37
38
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41 <tr>
42 <td id="breadcrumb">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44 <a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45 Creating Libraries
46 </td>
47 <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
48 </tr>
49 </table>
50 <!-- END BREADCRUMB -->
51
52 <br clear="all" />
53
54
55 <!-- START CONTENT -->
56 <div id="content">
57
58 <h1>Creating Libraries</h1>
59
60 <p>When we use the term "Libraries" we are normally referring to the classes that are located in the <kbd>libraries</kbd>
61 directory and described in the Class Reference of this user guide.  In this case, however, we will instead describe how you can create
62 your own libraries within your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
63 and the global framework resources.</p>
64
65 <p>As an added bonus, CodeIgniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
66 to an existing library. Or you can even replace native libraries just by placing identically named versions in your <dfn>application/libraries</dfn> folder.</p>
67
68 <p>In summary:</p>
69
70 <ul>
71 <li>You can create entirely new libraries.</li>
72 <li>You can extend native libraries.</li>
73 <li>You can replace native libraries.</li>
74 </ul>
75
76 <p>The page below explains these three concepts in detail.</p>
77
78 <p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes.  All other classes are able to be replaced/extended.</p>
79
80
81 <h2>Storage</h2>
82
83 <p>Your library classes should be placed within your <dfn>application/libraries</dfn> folder, as this is where CodeIgniter will look for them when
84 they are initialized.</p>
85
86
87 <h2>Naming Conventions</h2>
88
89 <ul>
90 <li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
91 <li>Class declarations must be capitalized. For example:&nbsp;  <kbd>class Myclass</kbd></li>
92 <li>Class names and file names must match.</li>
93 </ul>
94
95
96 <h2>The Class File</h2>
97
98 <p>Classes should have this basic prototype (Note:  We are using the name <kbd>Someclass</kbd> purely as an example):</p>
99
100 <code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
101 <br /><br />
102 class Someclass {<br />
103 <br />
104 &nbsp;&nbsp;&nbsp;&nbsp;public function some_function()<br />
105 &nbsp;&nbsp;&nbsp;&nbsp;{<br />
106 &nbsp;&nbsp;&nbsp;&nbsp;}<br />
107 }<br /><br />
108 /* End of file Someclass.php */</code>
109
110
111 <h2>Using Your Class</h2>
112
113 <p>From within any of your <a href="controllers.html">Controller</a> functions you can initialize your class using the standard:</p>
114
115 <code>$this->load->library('<kbd>someclass</kbd>');</code>
116
117 <p>Where <em>someclass</em> is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case.
118 CodeIgniter doesn't care.</p>
119
120 <p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>
121
122 <code>$this-><kbd>someclass</kbd>->some_function();&nbsp; // Object instances will always be lower case
123 </code>
124
125
126
127 <h2>Passing Parameters When Initializing Your Class</h2>
128
129 <p>In the library loading function you can dynamically pass data as an array via the second parameter and it will be passed to your class
130 constructor:</p>
131
132 <code>
133 $params = array('type' => 'large', 'color' => 'red');<br />
134 <br />
135 $this->load->library('Someclass', <kbd>$params</kbd>);</code>
136
137 <p>If you use this feature you must set up your class constructor to expect data:</p>
138
139 <code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');<br />
140 <br />
141 class Someclass {<br />
142 <br />
143 &nbsp;&nbsp;&nbsp;&nbsp;public function __construct($params)<br />
144 &nbsp;&nbsp;&nbsp;&nbsp;{<br />
145 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
146 &nbsp;&nbsp;&nbsp;&nbsp;}<br />
147 }<br /><br />
148 ?&gt;</code>
149
150 <p class="important">You can also pass parameters stored in a config file.  Simply create a config file named identically to the class <kbd>file name</kbd>
151 and store it in your <dfn>application/config/</dfn> folder.  Note that if you dynamically pass parameters as described above,
152 the config file option will not be available.</p>
153
154
155
156
157
158
159
160 <h2>Utilizing CodeIgniter Resources within Your Library</h2>
161
162
163 <p>To access CodeIgniter's native resources within your library use the <kbd>get_instance()</kbd> function.
164 This function returns the CodeIgniter super object.</p>
165
166 <p>Normally from within your controller functions you will call any of the available CodeIgniter functions using the <kbd>$this</kbd> construct:</p>
167
168 <code>
169 <strong>$this</strong>->load->helper('url');<br />
170 <strong>$this</strong>->load->library('session');<br />
171 <strong>$this</strong>->config->item('base_url');<br />
172 etc.
173 </code>
174
175 <p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
176 If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:</p>
177
178
179 <p>First, assign the CodeIgniter object to a variable:</p>
180
181 <code>$CI =&amp; get_instance();</code>
182
183 <p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
184
185 <code>
186 $CI =&amp; get_instance();<br />
187 <br />
188 $CI->load->helper('url');<br />
189 $CI->load->library('session');<br />
190 $CI->config->item('base_url');<br />
191 etc.
192 </code>
193
194 <p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
195 <br /><br />
196 <var>$CI =&amp; get_instance();</var>
197 <br />
198 <br />
199 <kbd>This is very important.</kbd> Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.</p>
200
201
202 <h2>Replacing Native Libraries with Your Versions</h2>
203
204 <p>Simply by naming your class files identically to a native library will cause CodeIgniter to use it instead of the native one. To use this
205 feature you must name the file and the class declaration exactly the same as the native library.  For example, to replace the native <kbd>Email</kbd> library
206 you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
207
208 <code>
209 class CI_Email {<br /><br />
210
211 }</code>
212
213 <p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>
214
215 <p>To load your library you'll see the standard loading function:</p>
216
217 <code>$this->load->library('<kbd>email</kbd>');</code>
218
219 <p class="important"><strong>Note:</strong> At this time the Database classes can not be replaced with your own versions.</p>
220
221
222 <h2>Extending Native Libraries</h2>
223
224 <p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
225 it's overkill to replace the entire library with your version.  In this case it's better to simply extend the class.
226 Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
227
228 <ul>
229 <li>The class declaration must extend the parent class.</li>
230 <li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable.  See below.).</li>
231 </ul>
232
233 <p>For example, to extend the native <kbd>Email</kbd> class you'll create a file named <dfn>application/libraries/</dfn><kbd>MY_Email.php</kbd>, and declare your class with:</p>
234
235 <code>
236 class MY_Email extends CI_Email {<br /><br />
237
238 }</code>
239
240 <p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
241
242
243 <code>
244 class MY_Email extends CI_Email {<br />
245 <br />
246 &nbsp;&nbsp;&nbsp;&nbsp;public function __construct()<br />
247 &nbsp;&nbsp;&nbsp;&nbsp;{<br />
248 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::__construct();<br />
249 &nbsp;&nbsp;&nbsp;&nbsp;}<br />
250 }</code>
251
252
253 <h3>Loading Your Sub-class</h3>
254
255 <p>To load your sub-class you'll use the standard syntax normally used.  DO NOT include your prefix.  For example,
256 to load the example above, which extends the Email class, you will use:</p>
257
258 <code>$this->load->library('<kbd>email</kbd>');</code>
259
260 <p>Once loaded you will use the class variable as you normally would for the class you are extending.  In the case of
261 the email class all calls will use:</p>
262
263
264 <code>$this-><kbd>email</kbd>->some_function();</code>
265
266
267 <h3>Setting Your Own Prefix</h3>
268
269 <p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
270
271 <code>$config['subclass_prefix'] = 'MY_';</code>
272
273 <p>Please note that all native CodeIgniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>
274
275
276
277 </div>
278 <!-- END CONTENT -->
279
280
281 <div id="footer">
282 <p>
283 Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using CodeIgniter Libraries</a>
284 &nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
285 <a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
286 <a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
287 Next Topic:&nbsp;&nbsp;<a href="drivers.html">Using CodeIgniter Drivers</a>
288 </p>
289 <p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
290 </div>
291
292 </body>
293 </html>