admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
admin | 0518661 | 2006-10-28 03:28:01 +0000 | [diff] [blame^] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
|
admin | 3cbadc1 | 2006-10-11 19:39:27 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Creating 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <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>
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 66 | directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create
|
| 67 | your own libraries within your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
|
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 68 | and the global framework resources.</p>
|
| 69 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 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
|
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 71 | to 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 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 81 | <p>The page below explains these three concepts in detail.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 82 |
|
admin | 79c643d | 2006-09-28 07:26:21 +0000 | [diff] [blame] | 83 | <p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes,
|
| 84 | nor can the main Controller class. All other classes are able to be replaced/extended.</p>
|
| 85 |
|
| 86 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 87 | <h2>Storage</h2>
|
| 88 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 89 | <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
|
admin | 33de9a1 | 2006-09-28 06:50:16 +0000 | [diff] [blame] | 90 | they are initialized.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 91 |
|
| 92 |
|
| 93 | <h2>Naming Conventions</h2>
|
| 94 |
|
| 95 | <ul>
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 96 | <li>File names must be capitalized. For example: <dfn>Myclass.php</dfn></li>
|
| 97 | <li>Class declarations must be capitalized. For example: <kbd>class Myclass</kbd></li>
|
| 98 | <li>Class names and file names must match.</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 99 | </ul>
|
| 100 |
|
| 101 |
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 102 | <h2>The Class File</h2>
|
| 103 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 104 | <p>Classes should have this basic prototype (Note: We are using the name <kbd>Someclass</kbd> purely as an example):</p>
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 105 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 106 | <code><?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 107 | <br /><br />
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 108 | class Someclass {<br />
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 109 | <br />
|
| 110 | function some_function()<br />
|
| 111 | {<br />
|
| 112 | }<br />
|
| 113 | }<br /><br />
|
| 114 | ?></code>
|
| 115 |
|
| 116 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 117 | <h2>Using Your Class</h2>
|
| 118 |
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 119 | <p>From within any of your <a href="controllers.html">Controller</a> functions you can initialize your class using the standard:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 120 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 121 | <code>$this->load->library('<kbd>someclass</kbd>');</code>
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 122 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 123 | <p>Where <em>someclass</em> is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case.
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 124 | Code Igniter doesn't care.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 125 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 126 | <p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 127 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 128 | <code>$this-><kbd>someclass</kbd>->some_function(); // Object instances will always be lower case
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 129 | </code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 130 |
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 131 |
|
| 132 |
|
| 133 | <h2>Passing Parameters When Initializing Your Class</h2>
|
| 134 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 135 | <p>In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 136 | constructor:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 137 |
|
| 138 | <code>
|
| 139 | $params = array('type' => 'large', 'color' => 'red');<br />
|
| 140 | <br />
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 141 | $this->load->library('Someclass', <kbd>$params</kbd>);</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 142 |
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 143 | <p>If you use this feature you must set up your class constructor to expect data:</p>
|
| 144 |
|
| 145 | <code><?php if (!defined('BASEPATH')) exit('No direct script access allowed');<br />
|
| 146 | <br />
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 147 | class Someclass {<br />
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 148 | <br />
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 149 | function Someclass($params)<br />
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 150 | {<br />
|
| 151 | // Do something with $params<br />
|
| 152 | }<br />
|
| 153 | }<br /><br />
|
| 154 | ?></code>
|
| 155 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 156 | <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>
|
| 157 | and store it in your <dfn>application/config/</dfn> folder. Note that if you dynamically pass parameters as described above,
|
| 158 | the config file option will not be available.</p>
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 164 |
|
| 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.
|
| 170 | This function returns the Code Igniter super object.</p>
|
| 171 |
|
admin | ebbe383 | 2006-09-26 06:02:47 +0000 | [diff] [blame] | 172 | <p>Normally from within your controller functions you will call any of the available Code Igniter functions using the <kbd>$this</kbd> construct:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 173 |
|
| 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 />
|
| 178 | etc.
|
| 179 | </code>
|
| 180 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 181 | <p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 182 | If 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 |
|
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 187 | <code>$CI =& get_instance();</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 188 |
|
| 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>
|
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 192 | $CI =& get_instance();<br /><br />
|
| 193 | $CI->load->helper('url');<br />
|
| 194 | $CI->load->library('session');<br />
|
| 195 | $CI->config->item('base_url');<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 196 | etc.
|
| 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 />
|
admin | 88a8ad1 | 2006-10-07 03:16:32 +0000 | [diff] [blame] | 201 | <var>$CI =& get_instance();</var>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 202 | <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 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 206 | <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
|
| 209 | 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
|
admin | 79c643d | 2006-09-28 07:26:21 +0000 | [diff] [blame] | 210 | you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 211 |
|
| 212 | <code>
|
| 213 | class 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 222 |
|
admin | 79c643d | 2006-09-28 07:26:21 +0000 | [diff] [blame] | 223 | <p class="important"><strong>Note:</strong> At thit time the Database classes can not be replaced with your own versions.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 224 |
|
| 225 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 226 | <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
|
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 229 | it's overkill to replace the entire library with your version. In this case it's better to simply extend the class.
|
| 230 | Extending a class is nearly identical to replacing a class with a couple exceptions:</p>
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 231 |
|
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 232 | <ul>
|
| 233 | <li>The class declaration must extend the parent class.</li>
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 234 | <li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable. See below.).</li>
|
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 235 | </ul>
|
| 236 |
|
| 237 | <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>
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 238 |
|
| 239 | <code>
|
| 240 | class MY_Email extends CI_Email {<br /><br />
|
| 241 |
|
| 242 | }</code>
|
| 243 |
|
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 244 | <p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>
|
| 245 |
|
| 246 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 247 | <code>
|
| 248 | class MY_Email extends CI_Email {<br />
|
| 249 | <br />
|
| 250 | function My_Email()<br />
|
| 251 | {<br />
|
| 252 | parent::CI_Email();<br />
|
| 253 | }<br />
|
| 254 | }</code>
|
| 255 |
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 256 |
|
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 257 | <h3>Loading Your Sub-class</h3>
|
| 258 |
|
| 259 | <p>To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example,
|
| 260 | to load the example above, which extends the Email class, you will use:</p>
|
| 261 |
|
| 262 | <code>$this->load->library('<kbd>email</kbd>');</code>
|
admin | e1bf942 | 2006-09-28 07:15:05 +0000 | [diff] [blame] | 263 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 264 | <p>Once loaded you will use the class variable as you normally would for the class you are extending. In the case of
|
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 265 | the email class all calls will use:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 266 |
|
| 267 |
|
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 268 | <code>$this-><kbd>email</kbd>->some_function();</code>
|
admin | 79c643d | 2006-09-28 07:26:21 +0000 | [diff] [blame] | 269 |
|
admin | 0625e19 | 2006-10-20 22:16:54 +0000 | [diff] [blame] | 270 |
|
| 271 | <h3>Setting Your Own Prefix</h3>
|
| 272 |
|
| 273 | <p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>
|
| 274 |
|
| 275 | <code>$config['subclass_prefix'] = 'MY_';</code>
|
| 276 |
|
| 277 | <p>Please note that all native Code Igniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>
|
| 278 |
|
| 279 |
|
| 280 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 281 | </div>
|
| 282 | <!-- END CONTENT -->
|
| 283 |
|
| 284 |
|
| 285 | <div id="footer">
|
| 286 | <p>
|
admin | a634e56 | 2006-08-25 22:19:55 +0000 | [diff] [blame] | 287 | Previous Topic: <a href="libraries.html">Using Code Igniter Libraries</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 288 | ·
|
| 289 | <a href="#top">Top of Page</a> ·
|
| 290 | <a href="../index.html">User Guide Home</a> ·
|
admin | a634e56 | 2006-08-25 22:19:55 +0000 | [diff] [blame] | 291 | Next Topic: <a href="core_classes.html">Creating Core System Classes</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 292 | <p>
|
| 293 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 294 | </div>
|
| 295 |
|
| 296 | </body>
|
| 297 | </html> |