blob: 64300929a926000708d17bdb209a8a309f48aa4f [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
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00006
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' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000026
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>
Derek Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</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">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000049<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
Derek Allardd2df9bc2007-04-15 17:41:17 +000070<p>As an added bonus, CodeIgniter 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
Derek Allardd2df9bc2007-04-15 17:41:17 +000089<p>Your library classes should be placed within your <dfn>application/libraries</dfn> folder, as this is where CodeIgniter 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.
Derek Allardd2df9bc2007-04-15 17:41:17 +0000124CodeIgniter 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
Derek Allardd2df9bc2007-04-15 17:41:17 +0000166<h2>Utilizing CodeIgniter Resources within Your Library</h2>
adminb0dd10f2006-08-25 17:25:49 +0000167
168
Derek Allardd2df9bc2007-04-15 17:41:17 +0000169<p>To access CodeIgniter's native resources within your library use the <kbd>get_instance()</kbd> function.
170This function returns the CodeIgniter super object.</p>
adminb0dd10f2006-08-25 17:25:49 +0000171
Derek Allardd2df9bc2007-04-15 17:41:17 +0000172<p>Normally from within your controller functions you will call any of the available CodeIgniter 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.
Derek Allardd2df9bc2007-04-15 17:41:17 +0000182If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:</p>
adminb0dd10f2006-08-25 17:25:49 +0000183
184
Derek Allardd2df9bc2007-04-15 17:41:17 +0000185<p>First, assign the CodeIgniter object to a variable:</p>
adminb0dd10f2006-08-25 17:25:49 +0000186
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 />
Rick Ellis1f852632007-06-09 00:25:57 +0000203<kbd>This is very important.</kbd> Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.
204<br /><br />
205<kbd>Also, please note:</kbd> If you are running PHP 4 it's usually best to avoid calling <dfn>get_instance()</dfn>
206from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors
207since objects do not exist until the class is fully instantiated.</p>
adminb0dd10f2006-08-25 17:25:49 +0000208
209
admine1bf9422006-09-28 07:15:05 +0000210<h2>Replacing Native Libraries with Your Versions</h2>
211
Derek Allardd2df9bc2007-04-15 17:41:17 +0000212<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
admine1bf9422006-09-28 07:15:05 +0000213feature 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 +0000214you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
admine1bf9422006-09-28 07:15:05 +0000215
216<code>
217class CI_Email {<br /><br />
218
219}</code>
220
221<p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>
222
223<p>To load your library you'll see the standard loading function:</p>
224
225<code>$this->load->library('<kbd>email</kbd>');</code>
adminb0dd10f2006-08-25 17:25:49 +0000226
Derek Allard7a1f5162007-01-27 18:02:09 +0000227<p class="important"><strong>Note:</strong> At this time the Database classes can not be replaced with your own versions.</p>
adminb0dd10f2006-08-25 17:25:49 +0000228
229
admine1bf9422006-09-28 07:15:05 +0000230<h2>Extending Native Libraries</h2>
231
232<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 +0000233it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
234Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
admine1bf9422006-09-28 07:15:05 +0000235
admin0625e192006-10-20 22:16:54 +0000236<ul>
237<li>The class declaration must extend the parent class.</li>
admine334c472006-10-21 19:44:22 +0000238<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 +0000239</ul>
240
241<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 +0000242
243<code>
244class MY_Email extends CI_Email {<br /><br />
245
246}</code>
247
admin0625e192006-10-20 22:16:54 +0000248<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
249
250
admine1bf9422006-09-28 07:15:05 +0000251<code>
252class MY_Email extends CI_Email {<br />
253<br />
254&nbsp;&nbsp;&nbsp;&nbsp;function My_Email()<br />
255&nbsp;&nbsp;&nbsp;&nbsp;{<br />
256&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::CI_Email();<br />
257&nbsp;&nbsp;&nbsp;&nbsp;}<br />
258}</code>
259
admine1bf9422006-09-28 07:15:05 +0000260
admin0625e192006-10-20 22:16:54 +0000261<h3>Loading Your Sub-class</h3>
262
263<p>To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example,
264to load the example above, which extends the Email class, you will use:</p>
265
266<code>$this->load->library('<kbd>email</kbd>');</code>
admine1bf9422006-09-28 07:15:05 +0000267
admine334c472006-10-21 19:44:22 +0000268<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 +0000269the email class all calls will use:
adminb0dd10f2006-08-25 17:25:49 +0000270
271
admincf493902006-10-10 07:12:31 +0000272<code>$this-><kbd>email</kbd>->some_function();</code>
admin79c643d2006-09-28 07:26:21 +0000273
admin0625e192006-10-20 22:16:54 +0000274
275<h3>Setting Your Own Prefix</h3>
276
277<p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
278
279<code>$config['subclass_prefix'] = 'MY_';</code>
280
Derek Allardd2df9bc2007-04-15 17:41:17 +0000281<p>Please note that all native CodeIgniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>
admin0625e192006-10-20 22:16:54 +0000282
283
284
adminb0dd10f2006-08-25 17:25:49 +0000285</div>
286<!-- END CONTENT -->
287
288
289<div id="footer">
290<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000291Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using CodeIgniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000292&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
293<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
294<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000295Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
adminb0dd10f2006-08-25 17:25:49 +0000296<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000297<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000298</div>
299
300</body>
301</html>