blob: a2d40f95de5030185289bb4e7708fade65a6bbef [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>
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>
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
81
adminb0dd10f2006-08-25 17:25:49 +000082
83<h2>Storage</h2>
84
adminebbe3832006-09-26 06:02:47 +000085<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 +000086they are initialized.</p>
adminb0dd10f2006-08-25 17:25:49 +000087
88
89<h2>Naming Conventions</h2>
90
91<ul>
adminebbe3832006-09-26 06:02:47 +000092<li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
93<li>Class declarations must be capitalized. For example:&nbsp; <kbd>class Myclass</kbd></li>
94<li>Class names and file names must match.</li>
adminb0dd10f2006-08-25 17:25:49 +000095</ul>
96
97
adminebbe3832006-09-26 06:02:47 +000098<h2>The Class File</h2>
99
100<p>Classes should have this basic prototype (Note: We are using the name <kbd>Myclass</kbd> purely as an example):</p>
101
102<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
adminebbe3832006-09-26 06:02:47 +0000103<br /><br />
104class Myclass {<br />
105<br />
106&nbsp;&nbsp;&nbsp;&nbsp;function some_function()<br />
107&nbsp;&nbsp;&nbsp;&nbsp;{<br />
108&nbsp;&nbsp;&nbsp;&nbsp;}<br />
109}<br /><br />
110?&gt;</code>
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
128
129<h2>Passing Parameters When Initializing Your Class</h2>
130
131<p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
132constructor:</p>
adminb0dd10f2006-08-25 17:25:49 +0000133
134<code>
135$params = array('type' => 'large', 'color' => 'red');<br />
136<br />
adminebbe3832006-09-26 06:02:47 +0000137$this->load->library('Myclass', <kbd>$params</kbd>);</code>
adminb0dd10f2006-08-25 17:25:49 +0000138
adminebbe3832006-09-26 06:02:47 +0000139<p>If you use this feature you must set up your class constructor to expect data:</p>
140
141<code>&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
142<br />
143// Initialize the class<br />
144$obj =& get_instance();<br />
145$obj->init_class('Myclass');
146<br /><br />
147class Myclass {<br />
148<br />
149&nbsp;&nbsp;&nbsp;&nbsp;function Myclass($params)<br />
150&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
admin33de9a12006-09-28 06:50:16 +0000156<p>You can also pass parameters stored in a config file. Simply create a config file named identically to the class filename.</p>
adminebbe3832006-09-26 06:02:47 +0000157
158
159
160
161
adminb0dd10f2006-08-25 17:25:49 +0000162
163
164<h2>Utilizing Code Igniter Resources within Your Library</h2>
165
166
167<p>To access Code Igniter's native resources within your library use the <kbd>get_instance()</kbd> function.
168This function returns the Code Igniter super object.</p>
169
adminebbe3832006-09-26 06:02:47 +0000170<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 +0000171
172<code>
173<strong>$this</strong>->load->helper('url');<br />
174<strong>$this</strong>->load->library('session');<br />
175<strong>$this</strong>->config->item('base_url');<br />
176etc.
177</code>
178
179<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
180If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:</p>
181
182
183<p>First, assign the Code Igniter object to a variable:</p>
184
185<code>$obj =& get_instance();</code>
186
187<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
188
189<code>
190$obj =& get_instance();<br /><br />
191$obj->load->helper('url');<br />
192$obj->load->library('session');<br />
193$obj->config->item('base_url');<br />
194etc.
195</code>
196
197<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
198<br /><br />
199<var>$obj =& get_instance();</var>
200<br /><br />
201<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>
202
203
204
205
206
207
208
209</div>
210<!-- END CONTENT -->
211
212
213<div id="footer">
214<p>
admina634e562006-08-25 22:19:55 +0000215Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using Code Igniter Libraries</a>
adminb0dd10f2006-08-25 17:25:49 +0000216&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
217<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
218<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admina634e562006-08-25 22:19:55 +0000219Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
adminb0dd10f2006-08-25 17:25:49 +0000220<p>
221<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>
222</div>
223
224</body>
225</html>