Subversion-Projekte lars-tiefland.codeigniter

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
<head>
4
 
5
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
<title>Creating Libraries : CodeIgniter User Guide</title>
7
 
8
<style type='text/css' media='all'>@import url('../userguide.css');</style>
9
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
 
11
<script type="text/javascript" src="../nav/nav.js"></script>
12
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13
<script type="text/javascript" src="../nav/moo.fx.js"></script>
14
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
 
16
<meta http-equiv='expires' content='-1' />
17
<meta http-equiv= 'pragma' content='no-cache' />
18
<meta name='robots' content='all' />
19
<meta name='author' content='ExpressionEngine Dev Team' />
20
<meta name='description' content='CodeIgniter User Guide' />
21
 
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_darker.jpg" width="154" height="43" 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>
31
<td><h1>CodeIgniter User Guide Version 1.7.1</h1></td>
32
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33
</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">
43
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45
Creating Libraries
46
</td>
47
<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>
48
</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>
61
directory and described in the Class Reference of this user guide.  In this case, however, we will instead describe how you can create
62
your own libraries within your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
63
and the global framework resources.</p>
64
 
65
<p>As an added bonus, CodeIgniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
66
to an existing library. Or you can even replace native libraries just by placing identically named versions in your <dfn>application/libraries</dfn> folder.</p>
67
 
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
 
76
<p>The page below explains these three concepts in detail.</p>
77
 
78
<p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes,
79
nor can the Loader class in PHP 4.  All other classes are able to be replaced/extended.</p>
80
 
81
 
82
<h2>Storage</h2>
83
 
84
<p>Your library classes should be placed within your <dfn>application/libraries</dfn> folder, as this is where CodeIgniter will look for them when
85
they are initialized.</p>
86
 
87
 
88
<h2>Naming Conventions</h2>
89
 
90
<ul>
91
<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>
94
</ul>
95
 
96
 
97
<h2>The Class File</h2>
98
 
99
<p>Classes should have this basic prototype (Note:  We are using the name <kbd>Someclass</kbd> purely as an example):</p>
100
 
101
<code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
102
<br /><br />
103
class Someclass {<br />
104
<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
 
112
<h2>Using Your Class</h2>
113
 
114
<p>From within any of your <a href="controllers.html">Controller</a> functions you can initialize your class using the standard:</p>
115
 
116
<code>$this->load->library('<kbd>someclass</kbd>');</code>
117
 
118
<p>Where <em>someclass</em> is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case.
119
CodeIgniter doesn't care.</p>
120
 
121
<p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>
122
 
123
<code>$this-><kbd>someclass</kbd>->some_function();&nbsp; // Object instances will always be lower case
124
</code>
125
 
126
 
127
 
128
<h2>Passing Parameters When Initializing Your Class</h2>
129
 
130
<p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
131
constructor:</p>
132
 
133
<code>
134
$params = array('type' => 'large', 'color' => 'red');<br />
135
<br />
136
$this->load->library('Someclass', <kbd>$params</kbd>);</code>
137
 
138
<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 />
142
class Someclass {<br />
143
<br />
144
&nbsp;&nbsp;&nbsp;&nbsp;function Someclass($params)<br />
145
&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
 
151
<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>
152
and store it in your <dfn>application/config/</dfn> folder.  Note that if you dynamically pass parameters as described above,
153
the config file option will not be available.</p>
154
 
155
 
156
 
157
 
158
 
159
 
160
 
161
<h2>Utilizing CodeIgniter Resources within Your Library</h2>
162
 
163
 
164
<p>To access CodeIgniter's native resources within your library use the <kbd>get_instance()</kbd> function.
165
This function returns the CodeIgniter super object.</p>
166
 
167
<p>Normally from within your controller functions you will call any of the available CodeIgniter functions using the <kbd>$this</kbd> construct:</p>
168
 
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 />
173
etc.
174
</code>
175
 
176
<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
177
If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:</p>
178
 
179
 
180
<p>First, assign the CodeIgniter object to a variable:</p>
181
 
182
<code>$CI =&amp; get_instance();</code>
183
 
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>
187
$CI =&amp; get_instance();<br />
188
<br />
189
$CI->load->helper('url');<br />
190
$CI->load->library('session');<br />
191
$CI->config->item('base_url');<br />
192
etc.
193
</code>
194
 
195
<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
196
<br /><br />
197
<var>$CI =&amp; get_instance();</var>
198
<br />
199
<br />
200
<kbd>This is very important.</kbd> Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.
201
<br /><br />
202
<kbd>Also, please note:</kbd> If you are running PHP 4 it's usually best to avoid calling <dfn>get_instance()</dfn>
203
from within your class constructors.  PHP 4 has trouble referencing the CI super object within application constructors
204
since objects do not exist until the class is fully instantiated.</p>
205
 
206
 
207
<h2>Replacing Native Libraries with Your Versions</h2>
208
 
209
<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
210
feature 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
211
you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
212
 
213
<code>
214
class CI_Email {<br /><br />
215
 
216
}</code>
217
 
218
<p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>
219
 
220
<p>To load your library you'll see the standard loading function:</p>
221
 
222
<code>$this->load->library('<kbd>email</kbd>');</code>
223
 
224
<p class="important"><strong>Note:</strong> At this time the Database classes can not be replaced with your own versions.</p>
225
 
226
 
227
<h2>Extending Native Libraries</h2>
228
 
229
<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
230
it's overkill to replace the entire library with your version.  In this case it's better to simply extend the class.
231
Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
232
 
233
<ul>
234
<li>The class declaration must extend the parent class.</li>
235
<li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable.  See below.).</li>
236
</ul>
237
 
238
<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>
239
 
240
<code>
241
class MY_Email extends CI_Email {<br /><br />
242
 
243
}</code>
244
 
245
<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
246
 
247
 
248
<code>
249
class MY_Email extends CI_Email {<br />
250
<br />
251
&nbsp;&nbsp;&nbsp;&nbsp;function My_Email()<br />
252
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
253
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::CI_Email();<br />
254
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
255
}</code>
256
 
257
 
258
<h3>Loading Your Sub-class</h3>
259
 
260
<p>To load your sub-class you'll use the standard syntax normally used.  DO NOT include your prefix.  For example,
261
to load the example above, which extends the Email class, you will use:</p>
262
 
263
<code>$this->load->library('<kbd>email</kbd>');</code>
264
 
265
<p>Once loaded you will use the class variable as you normally would for the class you are extending.  In the case of
266
the email class all calls will use:</p>
267
 
268
 
269
<code>$this-><kbd>email</kbd>->some_function();</code>
270
 
271
 
272
<h3>Setting Your Own Prefix</h3>
273
 
274
<p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
275
 
276
<code>$config['subclass_prefix'] = 'MY_';</code>
277
 
278
<p>Please note that all native CodeIgniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>
279
 
280
 
281
 
282
</div>
283
<!-- END CONTENT -->
284
 
285
 
286
<div id="footer">
287
<p>
288
Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using CodeIgniter Libraries</a>
289
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
290
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
291
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
292
Next Topic:&nbsp;&nbsp;<a href="core_classes.html">Creating Core System Classes</a>
293
</p>
294
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
295
</div>
296
 
297
</body>
298
</html>