blob: c8026d272b8d8e700fbbaa35085473d5fe675220 [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>
adminb0dd10f2006-08-25 17:25:49 +000011<script type="text/javascript" src="../scripts/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>
66directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create your own libraries within
67your <dfn>application</dfn> directory in order to maintain separation between your local resources and the global framework resources.</p>
68
69<h2>Storage</h2>
70
adminebbe3832006-09-26 06:02:47 +000071<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
72they are initialized. If your class is named identically to a native class from the <dfn>system/libraries</dfn> folder, your version
73will be used instead.</p>
adminb0dd10f2006-08-25 17:25:49 +000074
75
76<h2>Naming Conventions</h2>
77
78<ul>
adminebbe3832006-09-26 06:02:47 +000079<li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
80<li>Class declarations must be capitalized. For example:&nbsp; <kbd>class Myclass</kbd></li>
81<li>Class names and file names must match.</li>
adminb0dd10f2006-08-25 17:25:49 +000082</ul>
83
84
adminebbe3832006-09-26 06:02:47 +000085<h2>The Class File</h2>
86
87<p>Classes should have this basic prototype (Note: We are using the name <kbd>Myclass</kbd> purely as an example):</p>
88
89<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
90<br />
91// Initialize the class<br />
92$obj =& get_instance();<br />
93$obj->init_class('Myclass');
94<br /><br />
95class Myclass {<br />
96<br />
97&nbsp;&nbsp;&nbsp;&nbsp;function some_function()<br />
98&nbsp;&nbsp;&nbsp;&nbsp;{<br />
99&nbsp;&nbsp;&nbsp;&nbsp;}<br />
100}<br /><br />
101?&gt;</code>
102
103
104<p>You'll notice in the above example that the class is instantiated directly from the file itself using these two lines of code:</p>
105
106<code>$obj =& get_instance();<br />
107$obj->init_class(<kbd>'Myclass'</kbd>);</code>
108
109<p class="important">Make sure and submit your class name in the first parameter of the <kbd>$obj->init_class()</kbd> function. In the
110above example it is <kbd>Myclass</kbd></p>
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
adminebbe3832006-09-26 06:02:47 +0000117<code>$this->load->library('<kbd>Mclass</kbd>');</code>
118
119<p>Where <em>Myclass</em> is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case.
120Code Igniter doesn't care.</p>
adminb0dd10f2006-08-25 17:25:49 +0000121
122<p>Once loaded you can access your class using:</p>
123
adminebbe3832006-09-26 06:02:47 +0000124<code>$this-><kbd>myclass</kbd>->some_function();&nbsp; // Object instances will always be lower case
125</code>
adminb0dd10f2006-08-25 17:25:49 +0000126
adminebbe3832006-09-26 06:02:47 +0000127<h2>Setting a Different Class Variable Name</h2>
adminb0dd10f2006-08-25 17:25:49 +0000128
129
adminebbe3832006-09-26 06:02:47 +0000130<p>If you would like the object variable ($this->myclass) set to a different name you can specify it when initializing your class. For
131example, let's initialize it as <kbd>foobar</kbd>:</p>
adminb0dd10f2006-08-25 17:25:49 +0000132
adminebbe3832006-09-26 06:02:47 +0000133<code>$obj =& get_instance();<br />
134$obj->init_class('Myclass', <kbd>'foobar'</kbd>);</code>
135
136<p>In the above example you would still load your class like this:</p>
137
138<code>$this->load->library('<kbd>Mclass</kbd>');</code>
139
140<p>But you would use it like this:<p>
141
142<code>$this-><kbd>foobar</kbd>->function();</code>
143
144
145<h2>Passing Parameters When Initializing Your Class</h2>
146
147<p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
148constructor:</p>
adminb0dd10f2006-08-25 17:25:49 +0000149
150<code>
151$params = array('type' => 'large', 'color' => 'red');<br />
152<br />
adminebbe3832006-09-26 06:02:47 +0000153$this->load->library('Myclass', <kbd>$params</kbd>);</code>
adminb0dd10f2006-08-25 17:25:49 +0000154
adminebbe3832006-09-26 06:02:47 +0000155<p>If you use this feature you must set up your class constructor to expect data:</p>
156
157<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
158<br />
159// Initialize the class<br />
160$obj =& get_instance();<br />
161$obj->init_class('Myclass');
162<br /><br />
163class Myclass {<br />
164<br />
165&nbsp;&nbsp;&nbsp;&nbsp;function Myclass($params)<br />
166&nbsp;&nbsp;&nbsp;&nbsp;{<br />
167&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
168&nbsp;&nbsp;&nbsp;&nbsp;}<br />
169}<br /><br />
170?&gt;</code>
171
172<p>You can also pass parameters via the third parameter of the <dfn>$obj->init_class()</dfn> function:</p>
173
174<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
175<br />
176
177$params = array('type' => 'large', 'color' => 'red');<br /><br />
178
179// Initialize the class<br />
180$obj =& get_instance();<br />
181$obj->init_class('Myclass', 'myclass', $params);
182<br /><br />
183class Myclass {<br />
184<br />
185&nbsp;&nbsp;&nbsp;&nbsp;function Myclass($params)<br />
186&nbsp;&nbsp;&nbsp;&nbsp;{<br />
187&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
188&nbsp;&nbsp;&nbsp;&nbsp;}<br />
189}<br /><br />
190?&gt;</code>
191
192
193
194
195
adminb0dd10f2006-08-25 17:25:49 +0000196
197
198<h2>Utilizing Code Igniter Resources within Your Library</h2>
199
200
201<p>To access Code Igniter's native resources within your library use the <kbd>get_instance()</kbd> function.
202This function returns the Code Igniter super object.</p>
203
adminebbe3832006-09-26 06:02:47 +0000204<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 +0000205
206<code>
207<strong>$this</strong>->load->helper('url');<br />
208<strong>$this</strong>->load->library('session');<br />
209<strong>$this</strong>->config->item('base_url');<br />
210etc.
211</code>
212
213<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
214If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:</p>
215
216
217<p>First, assign the Code Igniter object to a variable:</p>
218
219<code>$obj =& get_instance();</code>
220
221<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
222
223<code>
224$obj =& get_instance();<br /><br />
225$obj->load->helper('url');<br />
226$obj->load->library('session');<br />
227$obj->config->item('base_url');<br />
228etc.
229</code>
230
231<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
232<br /><br />
233<var>$obj =& get_instance();</var>
234<br /><br />
235<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>
236
237
238
239
240
241
242
243</div>
244<!-- END CONTENT -->
245
246
247<div id="footer">
248<p>
admina634e562006-08-25 22:19:55 +0000249Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using Code Igniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000250&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
251<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
252<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000253Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
adminb0dd10f2006-08-25 17:25:49 +0000254<p>
255<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>
256</div>
257
258</body>
259</html>