blob: 12f8f9fc9a0d33787b1052b14eafd2ec1e6fcacb [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() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 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>
admine0cd6092006-10-20 01:00:31 +000036<td><h1>Code Igniter User Guide Version 1.5.0b3</h1></td>
admin3cbadc12006-10-11 19:39:27 +000037<td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</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>
admine334c472006-10-21 19:44:22 +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
admin33de9a12006-09-28 06:50:16 +000068and the global framework resources.</p>
69
admine334c472006-10-21 19:44:22 +000070<p>As an added bonus, Code Igniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
admin33de9a12006-09-28 06:50:16 +000071to 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
admin79c643d2006-09-28 07:26:21 +000083<p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes,
84nor can the main Controller class. All other classes are able to be replaced/extended.</p>
85
86
adminb0dd10f2006-08-25 17:25:49 +000087<h2>Storage</h2>
88
admine334c472006-10-21 19:44:22 +000089<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 +000090they are initialized.</p>
adminb0dd10f2006-08-25 17:25:49 +000091
92
93<h2>Naming Conventions</h2>
94
95<ul>
adminebbe3832006-09-26 06:02:47 +000096<li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
97<li>Class declarations must be capitalized. For example:&nbsp; <kbd>class Myclass</kbd></li>
98<li>Class names and file names must match.</li>
adminb0dd10f2006-08-25 17:25:49 +000099</ul>
100
101
adminebbe3832006-09-26 06:02:47 +0000102<h2>The Class File</h2>
103
admine1bf9422006-09-28 07:15:05 +0000104<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 +0000105
admine1bf9422006-09-28 07:15:05 +0000106<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
adminebbe3832006-09-26 06:02:47 +0000107<br /><br />
admine1bf9422006-09-28 07:15:05 +0000108class Someclass {<br />
adminebbe3832006-09-26 06:02:47 +0000109<br />
110&nbsp;&nbsp;&nbsp;&nbsp;function some_function()<br />
111&nbsp;&nbsp;&nbsp;&nbsp;{<br />
112&nbsp;&nbsp;&nbsp;&nbsp;}<br />
113}<br /><br />
114?&gt;</code>
115
116
adminb0dd10f2006-08-25 17:25:49 +0000117<h2>Using Your Class</h2>
118
adminebbe3832006-09-26 06:02:47 +0000119<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 +0000120
admine1bf9422006-09-28 07:15:05 +0000121<code>$this->load->library('<kbd>someclass</kbd>');</code>
adminebbe3832006-09-26 06:02:47 +0000122
admine334c472006-10-21 19:44:22 +0000123<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 +0000124Code Igniter doesn't care.</p>
adminb0dd10f2006-08-25 17:25:49 +0000125
admine1bf9422006-09-28 07:15:05 +0000126<p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>
adminb0dd10f2006-08-25 17:25:49 +0000127
admine1bf9422006-09-28 07:15:05 +0000128<code>$this-><kbd>someclass</kbd>->some_function();&nbsp; // Object instances will always be lower case
adminebbe3832006-09-26 06:02:47 +0000129</code>
adminb0dd10f2006-08-25 17:25:49 +0000130
adminebbe3832006-09-26 06:02:47 +0000131
132
133<h2>Passing Parameters When Initializing Your Class</h2>
134
admine334c472006-10-21 19:44:22 +0000135<p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
adminebbe3832006-09-26 06:02:47 +0000136constructor:</p>
adminb0dd10f2006-08-25 17:25:49 +0000137
138<code>
139$params = array('type' => 'large', 'color' => 'red');<br />
140<br />
admine1bf9422006-09-28 07:15:05 +0000141$this->load->library('Someclass', <kbd>$params</kbd>);</code>
adminb0dd10f2006-08-25 17:25:49 +0000142
adminebbe3832006-09-26 06:02:47 +0000143<p>If you use this feature you must set up your class constructor to expect data:</p>
144
145<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
146<br />
admine1bf9422006-09-28 07:15:05 +0000147class Someclass {<br />
adminebbe3832006-09-26 06:02:47 +0000148<br />
admine1bf9422006-09-28 07:15:05 +0000149&nbsp;&nbsp;&nbsp;&nbsp;function Someclass($params)<br />
adminebbe3832006-09-26 06:02:47 +0000150&nbsp;&nbsp;&nbsp;&nbsp;{<br />
151&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
152&nbsp;&nbsp;&nbsp;&nbsp;}<br />
153}<br /><br />
154?&gt;</code>
155
admine1bf9422006-09-28 07:15:05 +0000156<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>
157and store it in your <dfn>application/config/</dfn> folder. Note that if you dynamically pass parameters as described above,
158the config file option will not be available.</p>
adminebbe3832006-09-26 06:02:47 +0000159
160
161
162
163
adminb0dd10f2006-08-25 17:25:49 +0000164
165
166<h2>Utilizing Code Igniter Resources within Your Library</h2>
167
168
169<p>To access Code Igniter's native resources within your library use the <kbd>get_instance()</kbd> function.
170This function returns the Code Igniter super object.</p>
171
adminebbe3832006-09-26 06:02:47 +0000172<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 +0000173
174<code>
175<strong>$this</strong>->load->helper('url');<br />
176<strong>$this</strong>->load->library('session');<br />
177<strong>$this</strong>->config->item('base_url');<br />
178etc.
179</code>
180
admine334c472006-10-21 19:44:22 +0000181<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
adminb0dd10f2006-08-25 17:25:49 +0000182If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:</p>
183
184
185<p>First, assign the Code Igniter object to a variable:</p>
186
admin88a8ad12006-10-07 03:16:32 +0000187<code>$CI =& get_instance();</code>
adminb0dd10f2006-08-25 17:25:49 +0000188
189<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
190
191<code>
admin88a8ad12006-10-07 03:16:32 +0000192$CI =& get_instance();<br /><br />
193$CI->load->helper('url');<br />
194$CI->load->library('session');<br />
195$CI->config->item('base_url');<br />
adminb0dd10f2006-08-25 17:25:49 +0000196etc.
197</code>
198
199<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
200<br /><br />
admin88a8ad12006-10-07 03:16:32 +0000201<var>$CI =& get_instance();</var>
adminb0dd10f2006-08-25 17:25:49 +0000202<br /><br />
203<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>
204
205
admine1bf9422006-09-28 07:15:05 +0000206<h2>Replacing Native Libraries with Your Versions</h2>
207
208<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
209feature 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
admin79c643d2006-09-28 07:26:21 +0000210you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
admine1bf9422006-09-28 07:15:05 +0000211
212<code>
213class CI_Email {<br /><br />
214
215}</code>
216
217<p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>
218
219<p>To load your library you'll see the standard loading function:</p>
220
221<code>$this->load->library('<kbd>email</kbd>');</code>
adminb0dd10f2006-08-25 17:25:49 +0000222
admin79c643d2006-09-28 07:26:21 +0000223<p class="important"><strong>Note:</strong> At thit time the Database classes can not be replaced with your own versions.</p>
adminb0dd10f2006-08-25 17:25:49 +0000224
225
admine1bf9422006-09-28 07:15:05 +0000226<h2>Extending Native Libraries</h2>
227
228<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
admin0625e192006-10-20 22:16:54 +0000229it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
230Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
admine1bf9422006-09-28 07:15:05 +0000231
admin0625e192006-10-20 22:16:54 +0000232<ul>
233<li>The class declaration must extend the parent class.</li>
admine334c472006-10-21 19:44:22 +0000234<li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable. See below.).</li>
admin0625e192006-10-20 22:16:54 +0000235</ul>
236
237<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>
admine1bf9422006-09-28 07:15:05 +0000238
239<code>
240class MY_Email extends CI_Email {<br /><br />
241
242}</code>
243
admin0625e192006-10-20 22:16:54 +0000244<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
245
246
admine1bf9422006-09-28 07:15:05 +0000247<code>
248class MY_Email extends CI_Email {<br />
249<br />
250&nbsp;&nbsp;&nbsp;&nbsp;function My_Email()<br />
251&nbsp;&nbsp;&nbsp;&nbsp;{<br />
252&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::CI_Email();<br />
253&nbsp;&nbsp;&nbsp;&nbsp;}<br />
254}</code>
255
admine1bf9422006-09-28 07:15:05 +0000256
admin0625e192006-10-20 22:16:54 +0000257<h3>Loading Your Sub-class</h3>
258
259<p>To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example,
260to load the example above, which extends the Email class, you will use:</p>
261
262<code>$this->load->library('<kbd>email</kbd>');</code>
admine1bf9422006-09-28 07:15:05 +0000263
admine334c472006-10-21 19:44:22 +0000264<p>Once loaded you will use the class variable as you normally would for the class you are extending. In the case of
admincf493902006-10-10 07:12:31 +0000265the email class all calls will use:
adminb0dd10f2006-08-25 17:25:49 +0000266
267
admincf493902006-10-10 07:12:31 +0000268<code>$this-><kbd>email</kbd>->some_function();</code>
admin79c643d2006-09-28 07:26:21 +0000269
admin0625e192006-10-20 22:16:54 +0000270
271<h3>Setting Your Own Prefix</h3>
272
273<p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
274
275<code>$config['subclass_prefix'] = 'MY_';</code>
276
277<p>Please note that all native Code Igniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>
278
279
280
adminb0dd10f2006-08-25 17:25:49 +0000281</div>
282<!-- END CONTENT -->
283
284
285<div id="footer">
286<p>
admina634e562006-08-25 22:19:55 +0000287Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using Code Igniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000288&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
289<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
290<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000291Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
adminb0dd10f2006-08-25 17:25:49 +0000292<p>
293<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>
294</div>
295
296</body>
297</html>