blob: 3f168909df3089e71e708c9e229cab30ae3ca4ae [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>
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>
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
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
adminebbe3832006-09-26 06:02:47 +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
admine1bf9422006-09-28 07:15:05 +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
135<p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
136constructor:</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
181<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
182If 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
229it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.</p>
230
231<p>Extending a class is identical to replacing a class with one exception: The class declaration must extend the parent class
232and your new class must be prefixed with <kbd>MY_</kbd>. For example, to extend the native <kbd>Email</kbd> class
admin79c643d2006-09-28 07:26:21 +0000233you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
admine1bf9422006-09-28 07:15:05 +0000234
235<code>
236class MY_Email extends CI_Email {<br /><br />
237
238}</code>
239
admine1bf9422006-09-28 07:15:05 +0000240<code>
241class MY_Email extends CI_Email {<br />
242<br />
243&nbsp;&nbsp;&nbsp;&nbsp;function My_Email()<br />
244&nbsp;&nbsp;&nbsp;&nbsp;{<br />
245&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::CI_Email();<br />
246&nbsp;&nbsp;&nbsp;&nbsp;}<br />
247}</code>
248
249<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>
250
251<code>$this->load->library('<kbd>my_</kbd>email');</code>
252
admincf493902006-10-10 07:12:31 +0000253<p>Once loaded you will use the class variable as you normally would for the class you are extending. In the case of
254the email class all calls will use:
adminb0dd10f2006-08-25 17:25:49 +0000255
256
admincf493902006-10-10 07:12:31 +0000257<code>$this-><kbd>email</kbd>->some_function();</code>
admin79c643d2006-09-28 07:26:21 +0000258
adminb0dd10f2006-08-25 17:25:49 +0000259</div>
260<!-- END CONTENT -->
261
262
263<div id="footer">
264<p>
admina634e562006-08-25 22:19:55 +0000265Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using Code Igniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000266&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
267<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
268<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000269Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
adminb0dd10f2006-08-25 17:25:49 +0000270<p>
271<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>
272</div>
273
274</body>
275</html>