blob: d84cf2e236dc9be2d2edbc026584f5694a1e5a72 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
admin41a16852006-09-26 18:17:19 +000036<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Creating Libraries
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63<h1>Creating Libraries</h1>
64
65<p>When we use the term "Libraries" we are normally referring to the classes that are located in the <kbd>libraries</kbd>
admin33de9a12006-09-28 06:50:16 +000066directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create
67your own libraries within your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
68and the global framework resources.</p>
69
70<p>As an added bonus, Code Igniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
71to an existing library. Or you can even replace native libraries just by placing identically named versions in your <dfn>application/libraries</dfn> folder.
72
73<p>In summary:</p>
74
75<ul>
76<li>You can create entirely new libraries.</li>
77<li>You can extend native libraries.</li>
78<li>You can replace native libraries.</li>
79</ul>
80
admine1bf9422006-09-28 07:15:05 +000081<p>The page below explains these three concepts in detail.</p>
adminb0dd10f2006-08-25 17:25:49 +000082
83<h2>Storage</h2>
84
adminebbe3832006-09-26 06:02:47 +000085<p>Your library classes should be placed within your <dfn>application/libraries</dfn> folder, as this is where Code Igniter will look for them when
admin33de9a12006-09-28 06:50:16 +000086they are initialized.</p>
adminb0dd10f2006-08-25 17:25:49 +000087
88
89<h2>Naming Conventions</h2>
90
91<ul>
adminebbe3832006-09-26 06:02:47 +000092<li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
93<li>Class declarations must be capitalized. For example:&nbsp; <kbd>class Myclass</kbd></li>
94<li>Class names and file names must match.</li>
adminb0dd10f2006-08-25 17:25:49 +000095</ul>
96
97
adminebbe3832006-09-26 06:02:47 +000098<h2>The Class File</h2>
99
admine1bf9422006-09-28 07:15:05 +0000100<p>Classes should have this basic prototype (Note: We are using the name <kbd>Someclass</kbd> purely as an example):</p>
adminebbe3832006-09-26 06:02:47 +0000101
admine1bf9422006-09-28 07:15:05 +0000102<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
adminebbe3832006-09-26 06:02:47 +0000103<br /><br />
admine1bf9422006-09-28 07:15:05 +0000104class Someclass {<br />
adminebbe3832006-09-26 06:02:47 +0000105<br />
106&nbsp;&nbsp;&nbsp;&nbsp;function some_function()<br />
107&nbsp;&nbsp;&nbsp;&nbsp;{<br />
108&nbsp;&nbsp;&nbsp;&nbsp;}<br />
109}<br /><br />
110?&gt;</code>
111
112
adminb0dd10f2006-08-25 17:25:49 +0000113<h2>Using Your Class</h2>
114
adminebbe3832006-09-26 06:02:47 +0000115<p>From within any of your <a href="controllers.html">Controller</a> functions you can initialize your class using the standard:</p>
adminb0dd10f2006-08-25 17:25:49 +0000116
admine1bf9422006-09-28 07:15:05 +0000117<code>$this->load->library('<kbd>someclass</kbd>');</code>
adminebbe3832006-09-26 06:02:47 +0000118
admine1bf9422006-09-28 07:15:05 +0000119<p>Where <em>someclass</em> is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case.
adminebbe3832006-09-26 06:02:47 +0000120Code Igniter doesn't care.</p>
adminb0dd10f2006-08-25 17:25:49 +0000121
admine1bf9422006-09-28 07:15:05 +0000122<p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>
adminb0dd10f2006-08-25 17:25:49 +0000123
admine1bf9422006-09-28 07:15:05 +0000124<code>$this-><kbd>someclass</kbd>->some_function();&nbsp; // Object instances will always be lower case
adminebbe3832006-09-26 06:02:47 +0000125</code>
adminb0dd10f2006-08-25 17:25:49 +0000126
adminebbe3832006-09-26 06:02:47 +0000127
128
129<h2>Passing Parameters When Initializing Your Class</h2>
130
131<p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
132constructor:</p>
adminb0dd10f2006-08-25 17:25:49 +0000133
134<code>
135$params = array('type' => 'large', 'color' => 'red');<br />
136<br />
admine1bf9422006-09-28 07:15:05 +0000137$this->load->library('Someclass', <kbd>$params</kbd>);</code>
adminb0dd10f2006-08-25 17:25:49 +0000138
adminebbe3832006-09-26 06:02:47 +0000139<p>If you use this feature you must set up your class constructor to expect data:</p>
140
141<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
142<br />
admine1bf9422006-09-28 07:15:05 +0000143class Someclass {<br />
adminebbe3832006-09-26 06:02:47 +0000144<br />
admine1bf9422006-09-28 07:15:05 +0000145&nbsp;&nbsp;&nbsp;&nbsp;function Someclass($params)<br />
adminebbe3832006-09-26 06:02:47 +0000146&nbsp;&nbsp;&nbsp;&nbsp;{<br />
147&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
148&nbsp;&nbsp;&nbsp;&nbsp;}<br />
149}<br /><br />
150?&gt;</code>
151
admine1bf9422006-09-28 07:15:05 +0000152<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>
153and store it in your <dfn>application/config/</dfn> folder. Note that if you dynamically pass parameters as described above,
154the config file option will not be available.</p>
adminebbe3832006-09-26 06:02:47 +0000155
156
157
158
159
adminb0dd10f2006-08-25 17:25:49 +0000160
161
162<h2>Utilizing Code Igniter Resources within Your Library</h2>
163
164
165<p>To access Code Igniter's native resources within your library use the <kbd>get_instance()</kbd> function.
166This function returns the Code Igniter super object.</p>
167
adminebbe3832006-09-26 06:02:47 +0000168<p>Normally from within your controller functions you will call any of the available Code Igniter functions using the <kbd>$this</kbd> construct:</p>
adminb0dd10f2006-08-25 17:25:49 +0000169
170<code>
171<strong>$this</strong>->load->helper('url');<br />
172<strong>$this</strong>->load->library('session');<br />
173<strong>$this</strong>->config->item('base_url');<br />
174etc.
175</code>
176
177<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
178If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:</p>
179
180
181<p>First, assign the Code Igniter object to a variable:</p>
182
183<code>$obj =& get_instance();</code>
184
185<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
186
187<code>
188$obj =& get_instance();<br /><br />
189$obj->load->helper('url');<br />
190$obj->load->library('session');<br />
191$obj->config->item('base_url');<br />
192etc.
193</code>
194
195<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
196<br /><br />
197<var>$obj =& get_instance();</var>
198<br /><br />
199<kbd>This is very important.</kbd> Assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it.</p>
200
201
admine1bf9422006-09-28 07:15:05 +0000202<h2>Replacing Native Libraries with Your Versions</h2>
203
204<p>Simply by naming your class files identically to a native library will cause Code Igniter to use it instead of the native one. To use this
205feature 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
206you'll create a file named <dfn>application/libraries/email.php</dfn>, and declare your class with:</p>
207
208<code>
209class 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>
adminb0dd10f2006-08-25 17:25:49 +0000218
219
220
admine1bf9422006-09-28 07:15:05 +0000221<h2>Extending Native Libraries</h2>
222
223<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
224it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.</p>
225
226<p>Extending a class is identical to replacing a class with one exception: The class declaration must extend the parent class
227and your new class must be prefixed with <kbd>MY_</kbd>. For example, to extend the native <kbd>Email</kbd> class
228you'll create a file named <dfn>application/libraries/email.php</dfn>, and declare your class with:</p>
229
230<code>
231class MY_Email extends CI_Email {<br /><br />
232
233}</code>
234
235<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
236
237<code>
238class MY_Email extends CI_Email {<br />
239<br />
240&nbsp;&nbsp;&nbsp;&nbsp;function My_Email()<br />
241&nbsp;&nbsp;&nbsp;&nbsp;{<br />
242&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::CI_Email();<br />
243&nbsp;&nbsp;&nbsp;&nbsp;}<br />
244}</code>
245
246<p class="important"><strong>Important:</strong> To tell Code Igniter to load your sub-class you MUST include <kbd>my_</kbd> in the loading function:</p>
247
248<code>$this->load->library('<kbd>my_</kbd>email');</code>
249
adminb0dd10f2006-08-25 17:25:49 +0000250
251
252</div>
253<!-- END CONTENT -->
254
255
256<div id="footer">
257<p>
admina634e562006-08-25 22:19:55 +0000258Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using Code Igniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000259&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
260<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
261<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000262Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
adminb0dd10f2006-08-25 17:25:49 +0000263<p>
264<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
265</div>
266
267</body>
268</html>