blob: 5e0568243441f985e058c6c2348590e1ee3a3489 [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">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Creating Libraries</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>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
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.jpg" width="153" height="44" 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>
Derek Allard197d10b2008-02-12 04:20:38 +000031<td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</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">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Creating Libraries
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<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>
adminb0dd10f2006-08-25 17:25:49 +000048</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>
admine334c472006-10-21 19:44:22 +000061directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create
62your own libraries within your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
admin33de9a12006-09-28 06:50:16 +000063and the global framework resources.</p>
64
Derek Allardd2df9bc2007-04-15 17:41:17 +000065<p>As an added bonus, CodeIgniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
Derek Allardc6441282007-07-04 23:54:32 +000066to an existing library. Or you can even replace native libraries just by placing identically named versions in your <dfn>application/libraries</dfn> folder.</p>
admin33de9a12006-09-28 06:50:16 +000067
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
admine1bf9422006-09-28 07:15:05 +000076<p>The page below explains these three concepts in detail.</p>
adminb0dd10f2006-08-25 17:25:49 +000077
admin79c643d2006-09-28 07:26:21 +000078<p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes,
79nor can the main Controller class. All other classes are able to be replaced/extended.</p>
80
81
adminb0dd10f2006-08-25 17:25:49 +000082<h2>Storage</h2>
83
Derek Allardd2df9bc2007-04-15 17:41:17 +000084<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 +000085they are initialized.</p>
adminb0dd10f2006-08-25 17:25:49 +000086
87
88<h2>Naming Conventions</h2>
89
90<ul>
adminebbe3832006-09-26 06:02:47 +000091<li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
92<li>Class declarations must be capitalized. For example:&nbsp; <kbd>class Myclass</kbd></li>
93<li>Class names and file names must match.</li>
adminb0dd10f2006-08-25 17:25:49 +000094</ul>
95
96
adminebbe3832006-09-26 06:02:47 +000097<h2>The Class File</h2>
98
admine1bf9422006-09-28 07:15:05 +000099<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 +0000100
admine1bf9422006-09-28 07:15:05 +0000101<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
adminebbe3832006-09-26 06:02:47 +0000102<br /><br />
admine1bf9422006-09-28 07:15:05 +0000103class Someclass {<br />
adminebbe3832006-09-26 06:02:47 +0000104<br />
105&nbsp;&nbsp;&nbsp;&nbsp;function some_function()<br />
106&nbsp;&nbsp;&nbsp;&nbsp;{<br />
107&nbsp;&nbsp;&nbsp;&nbsp;}<br />
108}<br /><br />
109?&gt;</code>
110
111
adminb0dd10f2006-08-25 17:25:49 +0000112<h2>Using Your Class</h2>
113
adminebbe3832006-09-26 06:02:47 +0000114<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 +0000115
admine1bf9422006-09-28 07:15:05 +0000116<code>$this->load->library('<kbd>someclass</kbd>');</code>
adminebbe3832006-09-26 06:02:47 +0000117
admine334c472006-10-21 19:44:22 +0000118<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 +0000119CodeIgniter doesn't care.</p>
adminb0dd10f2006-08-25 17:25:49 +0000120
admine1bf9422006-09-28 07:15:05 +0000121<p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>
adminb0dd10f2006-08-25 17:25:49 +0000122
admine1bf9422006-09-28 07:15:05 +0000123<code>$this-><kbd>someclass</kbd>->some_function();&nbsp; // Object instances will always be lower case
adminebbe3832006-09-26 06:02:47 +0000124</code>
adminb0dd10f2006-08-25 17:25:49 +0000125
adminebbe3832006-09-26 06:02:47 +0000126
127
128<h2>Passing Parameters When Initializing Your Class</h2>
129
admine334c472006-10-21 19:44:22 +0000130<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 +0000131constructor:</p>
adminb0dd10f2006-08-25 17:25:49 +0000132
133<code>
134$params = array('type' => 'large', 'color' => 'red');<br />
135<br />
admine1bf9422006-09-28 07:15:05 +0000136$this->load->library('Someclass', <kbd>$params</kbd>);</code>
adminb0dd10f2006-08-25 17:25:49 +0000137
adminebbe3832006-09-26 06:02:47 +0000138<p>If you use this feature you must set up your class constructor to expect data:</p>
139
140<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
141<br />
admine1bf9422006-09-28 07:15:05 +0000142class Someclass {<br />
adminebbe3832006-09-26 06:02:47 +0000143<br />
admine1bf9422006-09-28 07:15:05 +0000144&nbsp;&nbsp;&nbsp;&nbsp;function Someclass($params)<br />
adminebbe3832006-09-26 06:02:47 +0000145&nbsp;&nbsp;&nbsp;&nbsp;{<br />
146&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
147&nbsp;&nbsp;&nbsp;&nbsp;}<br />
148}<br /><br />
149?&gt;</code>
150
admine1bf9422006-09-28 07:15:05 +0000151<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>
152and store it in your <dfn>application/config/</dfn> folder. Note that if you dynamically pass parameters as described above,
153the config file option will not be available.</p>
adminebbe3832006-09-26 06:02:47 +0000154
155
156
157
158
adminb0dd10f2006-08-25 17:25:49 +0000159
160
Derek Allardd2df9bc2007-04-15 17:41:17 +0000161<h2>Utilizing CodeIgniter Resources within Your Library</h2>
adminb0dd10f2006-08-25 17:25:49 +0000162
163
Derek Allardd2df9bc2007-04-15 17:41:17 +0000164<p>To access CodeIgniter's native resources within your library use the <kbd>get_instance()</kbd> function.
165This function returns the CodeIgniter super object.</p>
adminb0dd10f2006-08-25 17:25:49 +0000166
Derek Allardd2df9bc2007-04-15 17:41:17 +0000167<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 +0000168
169<code>
170<strong>$this</strong>->load->helper('url');<br />
171<strong>$this</strong>->load->library('session');<br />
172<strong>$this</strong>->config->item('base_url');<br />
173etc.
174</code>
175
admine334c472006-10-21 19:44:22 +0000176<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
Derek Allardd2df9bc2007-04-15 17:41:17 +0000177If 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 +0000178
179
Derek Allardd2df9bc2007-04-15 17:41:17 +0000180<p>First, assign the CodeIgniter object to a variable:</p>
adminb0dd10f2006-08-25 17:25:49 +0000181
admin88a8ad12006-10-07 03:16:32 +0000182<code>$CI =& get_instance();</code>
adminb0dd10f2006-08-25 17:25:49 +0000183
184<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
185
186<code>
admin88a8ad12006-10-07 03:16:32 +0000187$CI =& get_instance();<br /><br />
188$CI->load->helper('url');<br />
189$CI->load->library('session');<br />
190$CI->config->item('base_url');<br />
adminb0dd10f2006-08-25 17:25:49 +0000191etc.
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 />
admin88a8ad12006-10-07 03:16:32 +0000196<var>$CI =& get_instance();</var>
adminb0dd10f2006-08-25 17:25:49 +0000197<br /><br />
Rick Ellis1f852632007-06-09 00:25:57 +0000198<kbd>This is very important.</kbd> Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.
199<br /><br />
200<kbd>Also, please note:</kbd> If you are running PHP 4 it's usually best to avoid calling <dfn>get_instance()</dfn>
201from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors
202since objects do not exist until the class is fully instantiated.</p>
adminb0dd10f2006-08-25 17:25:49 +0000203
204
admine1bf9422006-09-28 07:15:05 +0000205<h2>Replacing Native Libraries with Your Versions</h2>
206
Derek Allardd2df9bc2007-04-15 17:41:17 +0000207<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 +0000208feature 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 +0000209you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
admine1bf9422006-09-28 07:15:05 +0000210
211<code>
212class CI_Email {<br /><br />
213
214}</code>
215
216<p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>
217
218<p>To load your library you'll see the standard loading function:</p>
219
220<code>$this->load->library('<kbd>email</kbd>');</code>
adminb0dd10f2006-08-25 17:25:49 +0000221
Derek Allard7a1f5162007-01-27 18:02:09 +0000222<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 +0000223
224
admine1bf9422006-09-28 07:15:05 +0000225<h2>Extending Native Libraries</h2>
226
227<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 +0000228it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
229Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
admine1bf9422006-09-28 07:15:05 +0000230
admin0625e192006-10-20 22:16:54 +0000231<ul>
232<li>The class declaration must extend the parent class.</li>
admine334c472006-10-21 19:44:22 +0000233<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 +0000234</ul>
235
236<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 +0000237
238<code>
239class MY_Email extends CI_Email {<br /><br />
240
241}</code>
242
admin0625e192006-10-20 22:16:54 +0000243<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
244
245
admine1bf9422006-09-28 07:15:05 +0000246<code>
247class MY_Email extends CI_Email {<br />
248<br />
249&nbsp;&nbsp;&nbsp;&nbsp;function My_Email()<br />
250&nbsp;&nbsp;&nbsp;&nbsp;{<br />
251&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::CI_Email();<br />
252&nbsp;&nbsp;&nbsp;&nbsp;}<br />
253}</code>
254
admine1bf9422006-09-28 07:15:05 +0000255
admin0625e192006-10-20 22:16:54 +0000256<h3>Loading Your Sub-class</h3>
257
258<p>To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example,
259to load the example above, which extends the Email class, you will use:</p>
260
261<code>$this->load->library('<kbd>email</kbd>');</code>
admine1bf9422006-09-28 07:15:05 +0000262
admine334c472006-10-21 19:44:22 +0000263<p>Once loaded you will use the class variable as you normally would for the class you are extending. In the case of
Derek Allardc6441282007-07-04 23:54:32 +0000264the email class all calls will use:</p>
adminb0dd10f2006-08-25 17:25:49 +0000265
266
admincf493902006-10-10 07:12:31 +0000267<code>$this-><kbd>email</kbd>->some_function();</code>
admin79c643d2006-09-28 07:26:21 +0000268
admin0625e192006-10-20 22:16:54 +0000269
270<h3>Setting Your Own Prefix</h3>
271
272<p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
273
274<code>$config['subclass_prefix'] = 'MY_';</code>
275
Derek Allardd2df9bc2007-04-15 17:41:17 +0000276<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 +0000277
278
279
adminb0dd10f2006-08-25 17:25:49 +0000280</div>
281<!-- END CONTENT -->
282
283
284<div id="footer">
285<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000286Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using CodeIgniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000287&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
288<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
289<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000290Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
Derek Allardc6441282007-07-04 23:54:32 +0000291</p>
Derek Jones7a9193a2008-01-21 18:39:20 +0000292<p><a href="http://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 +0000293</div>
294
295</body>
296</html>