blob: 4beb600da32e656ea8d19d9220b07359f49ea440 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Creating Libraries
3##################
4
5When we use the term "Libraries" we are normally referring to the
6classes that are located in the libraries directory and described in the
7Class Reference of this user guide. In this case, however, we will
8instead describe how you can create your own libraries within your
9application/libraries directory in order to maintain separation between
10your local resources and the global framework resources.
11
12As an added bonus, CodeIgniter permits your libraries to extend native
13classes if you simply need to add some functionality to an existing
14library. Or you can even replace native libraries just by placing
Andrey Andreev16a704c2012-11-09 17:25:00 +020015identically named versions in your *application/libraries* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050016
17In summary:
18
19- You can create entirely new libraries.
20- You can extend native libraries.
21- You can replace native libraries.
22
23The page below explains these three concepts in detail.
24
25.. note:: The Database classes can not be extended or replaced with your
26 own classes. All other classes are able to be replaced/extended.
27
28Storage
29=======
30
Andrey Andreev16a704c2012-11-09 17:25:00 +020031Your library classes should be placed within your *application/libraries*
32directory, as this is where CodeIgniter will look for them when they are
Derek Jones8ede1a22011-10-05 13:34:52 -050033initialized.
34
35Naming Conventions
36==================
37
38- File names must be capitalized. For example: Myclass.php
39- Class declarations must be capitalized. For example: class Myclass
40- Class names and file names must match.
41
42The Class File
43==============
44
Andrey Andreev20292312013-07-22 14:29:10 +030045Classes should have this basic prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Derek Jones9713cce2011-10-05 17:26:43 -050047 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
48
49 class Someclass {
50
Andrey Andreev16a704c2012-11-09 17:25:00 +020051 public function some_method()
52 {
53 }
Derek Jones9713cce2011-10-05 17:26:43 -050054 }
55
56 /* End of file Someclass.php */
Derek Jones8ede1a22011-10-05 13:34:52 -050057
Andrey Andreev20292312013-07-22 14:29:10 +030058.. note:: We are using the name Someclass purely as an example.
59
Derek Jones8ede1a22011-10-05 13:34:52 -050060Using Your Class
61================
62
Andrey Andreev16a704c2012-11-09 17:25:00 +020063From within any of your :doc:`Controller <controllers>` methods you
Derek Jones8ede1a22011-10-05 13:34:52 -050064can initialize your class using the standard::
65
66 $this->load->library('someclass');
67
68Where *someclass* is the file name, without the ".php" file extension.
69You can submit the file name capitalized or lower case. CodeIgniter
70doesn't care.
71
72Once loaded you can access your class using the lower case version::
73
Andrey Andreev16a704c2012-11-09 17:25:00 +020074 $this->someclass->some_method();  // Object instances will always be lower case
Derek Jones8ede1a22011-10-05 13:34:52 -050075
76Passing Parameters When Initializing Your Class
77===============================================
78
Andrey Andreev16a704c2012-11-09 17:25:00 +020079In the library loading method you can dynamically pass data as an
Derek Jones8ede1a22011-10-05 13:34:52 -050080array via the second parameter and it will be passed to your class
81constructor::
82
Derek Jones9713cce2011-10-05 17:26:43 -050083 $params = array('type' => 'large', 'color' => 'red');
84
Andrey Andreev20292312013-07-22 14:29:10 +030085 $this->load->library('someclass', $params);
Derek Jones8ede1a22011-10-05 13:34:52 -050086
87If you use this feature you must set up your class constructor to expect
88data::
89
Andrey Andreev16a704c2012-11-09 17:25:00 +020090 <?php defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jones9713cce2011-10-05 17:26:43 -050091
92 class Someclass {
93
Andrey Andreev16a704c2012-11-09 17:25:00 +020094 public function __construct($params)
95 {
96 // Do something with $params
97 }
Derek Jones9713cce2011-10-05 17:26:43 -050098 }
99
Derek Jones8ede1a22011-10-05 13:34:52 -0500100You can also pass parameters stored in a config file. Simply create a
101config file named identically to the class file name and store it in
Andrey Andreev16a704c2012-11-09 17:25:00 +0200102your *application/config/* directory. Note that if you dynamically pass
Derek Jones8ede1a22011-10-05 13:34:52 -0500103parameters as described above, the config file option will not be
104available.
105
106Utilizing CodeIgniter Resources within Your Library
107===================================================
108
109To access CodeIgniter's native resources within your library use the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200110``get_instance()`` method. This method returns the CodeIgniter super
Derek Jones8ede1a22011-10-05 13:34:52 -0500111object.
112
Andrey Andreev16a704c2012-11-09 17:25:00 +0200113Normally from within your controller methods you will call any of the
114available CodeIgniter methods using the ``$this`` construct::
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Derek Jones9713cce2011-10-05 17:26:43 -0500116 $this->load->helper('url');
117 $this->load->library('session');
118 $this->config->item('base_url');
119 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreev16a704c2012-11-09 17:25:00 +0200121``$this``, however, only works directly within your controllers, your
Derek Jones8ede1a22011-10-05 13:34:52 -0500122models, or your views. If you would like to use CodeIgniter's classes
123from within your own custom classes you can do so as follows:
124
125First, assign the CodeIgniter object to a variable::
126
127 $CI =& get_instance();
128
129Once you've assigned the object to a variable, you'll use that variable
Andrey Andreev16a704c2012-11-09 17:25:00 +0200130*instead* of ``$this``::
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Derek Jones9713cce2011-10-05 17:26:43 -0500132 $CI =& get_instance();
133
134 $CI->load->helper('url');
135 $CI->load->library('session');
136 $CI->config->item('base_url');
137 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev16a704c2012-11-09 17:25:00 +0200139.. note:: You'll notice that the above ``get_instance()`` function is being
Derek Jones8ede1a22011-10-05 13:34:52 -0500140 passed by reference::
141
142 $CI =& get_instance();
143
144 This is very important. Assigning by reference allows you to use the
145 original CodeIgniter object rather than creating a copy of it.
146
Andrey Andreev16a704c2012-11-09 17:25:00 +0200147However, since a library is a class, it would be better if you
148take full advantage of the OOP principles. So, in order to
149be able to use the CodeIgniter super-object in all of the class
150methods, you're encouraged to assign it to a property instead::
151
vlakoff024cfec2013-01-09 18:10:20 +0100152 class Example_library {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200153
vlakoff024cfec2013-01-09 18:10:20 +0100154 protected $CI;
Andrey Andreev16a704c2012-11-09 17:25:00 +0200155
vlakoff024cfec2013-01-09 18:10:20 +0100156 // We'll use a constructor, as you can't directly call a function
157 // from a property definition.
158 public function __construct()
159 {
160 // Assign the CodeIgniter super-object
161 $this->CI =& get_instance();
162 }
163
164 public function foo()
165 {
166 $this->CI->load->helper('url');
167 redirect();
168 }
169
170 public function bar()
171 {
172 echo $this->CI->config_item('base_url');
173 }
174
Andrey Andreev16a704c2012-11-09 17:25:00 +0200175 }
176
Derek Jones8ede1a22011-10-05 13:34:52 -0500177Replacing Native Libraries with Your Versions
178=============================================
179
180Simply by naming your class files identically to a native library will
181cause CodeIgniter to use it instead of the native one. To use this
182feature you must name the file and the class declaration exactly the
183same as the native library. For example, to replace the native Email
Andrey Andreev16a704c2012-11-09 17:25:00 +0200184library you'll create a file named *application/libraries/Email.php*,
185and declare your class with::
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
Derek Jones9713cce2011-10-05 17:26:43 -0500187 class CI_Email {
188
189 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
191Note that most native classes are prefixed with CI\_.
192
Andrey Andreev16a704c2012-11-09 17:25:00 +0200193To load your library you'll see the standard loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195 $this->load->library('email');
196
197.. note:: At this time the Database classes can not be replaced with
198 your own versions.
199
200Extending Native Libraries
201==========================
202
203If all you need to do is add some functionality to an existing library -
Andrey Andreev16a704c2012-11-09 17:25:00 +0200204perhaps add a method or two - then it's overkill to replace the entire
Derek Jones8ede1a22011-10-05 13:34:52 -0500205library with your version. In this case it's better to simply extend the
206class. Extending a class is nearly identical to replacing a class with a
207couple exceptions:
208
209- The class declaration must extend the parent class.
210- Your new class name and filename must be prefixed with MY\_ (this
211 item is configurable. See below.).
212
213For example, to extend the native Email class you'll create a file named
Andrey Andreev16a704c2012-11-09 17:25:00 +0200214*application/libraries/MY_Email.php*, and declare your class with::
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
Derek Jones9713cce2011-10-05 17:26:43 -0500216 class MY_Email extends CI_Email {
217
218 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
Andrey Andreev1d571972012-03-07 11:49:35 +0200220If you need to use a constructor in your class make sure you
Derek Jones8ede1a22011-10-05 13:34:52 -0500221extend the parent constructor::
222
Derek Jones9713cce2011-10-05 17:26:43 -0500223 class MY_Email extends CI_Email {
224
Andrey Andreev1d571972012-03-07 11:49:35 +0200225 public function __construct($config = array())
226 {
227 parent::__construct($config);
228 }
229
Derek Jones9713cce2011-10-05 17:26:43 -0500230 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500231
Andrey Andreev16a704c2012-11-09 17:25:00 +0200232.. note:: Not all of the libraries have the same (or any) parameters
Andrey Andreev1d571972012-03-07 11:49:35 +0200233 in their constructor. Take a look at the library that you're
234 extending first to see how it should be implemented.
235
Derek Jones8ede1a22011-10-05 13:34:52 -0500236Loading Your Sub-class
237----------------------
238
239To load your sub-class you'll use the standard syntax normally used. DO
240NOT include your prefix. For example, to load the example above, which
241extends the Email class, you will use::
242
243 $this->load->library('email');
244
245Once loaded you will use the class variable as you normally would for
246the class you are extending. In the case of the email class all calls
247will use::
248
Andrey Andreev16a704c2012-11-09 17:25:00 +0200249 $this->email->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500250
251Setting Your Own Prefix
252-----------------------
253
254To set your own sub-class prefix, open your
Andrey Andreev16a704c2012-11-09 17:25:00 +0200255*application/config/config.php* file and look for this item::
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
257 $config['subclass_prefix'] = 'MY_';
258
259Please note that all native CodeIgniter libraries are prefixed with CI\_
Andrey Andreev16a704c2012-11-09 17:25:00 +0200260so DO NOT use that as your prefix.