1. Tatsuya Fukata
  2. CodeIgniter Reactor


CodeIgniter Reactor / user_guide / general / creating_libraries.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Creating Libraries : CodeIgniter User Guide</title>

<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />


<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<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>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<td><h1>CodeIgniter User Guide Version 2.0.2</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>

<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Creating Libraries
<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>

<br clear="all" />

<div id="content">

<h1>Creating Libraries</h1>

<p>When we use the term "Libraries" we are normally referring to the classes that are located in the <kbd>libraries</kbd>
directory 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 your <dfn>application/libraries</dfn> directory in order to maintain separation between your local resources
and the global framework resources.</p>

<p>As an added bonus, CodeIgniter permits your libraries to <kbd>extend</kbd> native classes if you simply need to add some functionality
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>

<p>In summary:</p>

<li>You can create entirely new libraries.</li>
<li>You can extend native libraries.</li>
<li>You can replace native libraries.</li>

<p>The page below explains these three concepts in detail.</p>

<p class="important"><strong>Note:</strong> The Database classes can not be extended or replaced with your own classes.  All other classes are able to be replaced/extended.</p>


<p>Your library classes should be placed within your <dfn>application/libraries</dfn> folder, as this is where CodeIgniter will look for them when
they are initialized.</p>

<h2>Naming Conventions</h2>

<li>File names must be capitalized. For example:&nbsp; <dfn>Myclass.php</dfn></li>
<li>Class declarations must be capitalized. For example:&nbsp;  <kbd>class Myclass</kbd></li>
<li>Class names and file names must match.</li>

<h2>The Class File</h2>

<p>Classes should have this basic prototype (Note:  We are using the name <kbd>Someclass</kbd> purely as an example):</p>

<code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
<br /><br />
class Someclass {<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;public function some_function()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br /><br />
/* End of file Someclass.php */</code>

<h2>Using Your Class</h2>

<p>From within any of your <a href="controllers.html">Controller</a> functions you can initialize your class using the standard:</p>


<p>Where <em>someclass</em> is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case.
CodeIgniter doesn't care.</p>

<p>Once loaded you can access your class using the <kbd>lower case</kbd> version:</p>

<code>$this-><kbd>someclass</kbd>->some_function();&nbsp; // Object instances will always be lower case

<h2>Passing Parameters When Initializing Your Class</h2>

<p>In the library loading function you can dynamically pass data as an array via the second parameter and it will be passed to your class

$params = array('type' => 'large', 'color' => 'red');<br />
<br />
$this->load->library('Someclass', <kbd>$params</kbd>);</code>

<p>If you use this feature you must set up your class constructor to expect data:</p>

<code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');<br />
<br />
class Someclass {<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;public function __construct($params)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Do something with $params<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br /><br />

<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>
and store it in your <dfn>application/config/</dfn> folder.  Note that if you dynamically pass parameters as described above,
the config file option will not be available.</p>

<h2>Utilizing CodeIgniter Resources within Your Library</h2>

<p>To access CodeIgniter's native resources within your library use the <kbd>get_instance()</kbd> function.
This function returns the CodeIgniter super object.</p>

<p>Normally from within your controller functions you will call any of the available CodeIgniter functions using the <kbd>$this</kbd> construct:</p>

<strong>$this</strong>->load->helper('url');<br />
<strong>$this</strong>->load->library('session');<br />
<strong>$this</strong>->config->item('base_url');<br />

<p><kbd>$this</kbd>, however, only works directly within your controllers, your models, or your views.
If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:</p>

<p>First, assign the CodeIgniter object to a variable:</p>

<code>$CI =&amp; get_instance();</code>

<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>

$CI =&amp; get_instance();<br />
<br />
$CI->load->helper('url');<br />
$CI->load->library('session');<br />
$CI->config->item('base_url');<br />

<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
<br /><br />
<var>$CI =&amp; get_instance();</var>
<br />
<br />
<kbd>This is very important.</kbd> Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.</p>

<h2>Replacing Native Libraries with Your Versions</h2>

<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
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
you'll create a file named <dfn>application/libraries/Email.php</dfn>, and declare your class with:</p>

class CI_Email {<br /><br />


<p>Note that most native classes are prefixed with <kbd>CI_</kbd>.</p>

<p>To load your library you'll see the standard loading function:</p>


<p class="important"><strong>Note:</strong> At this time the Database classes can not be replaced with your own versions.</p>

<h2>Extending Native Libraries</h2>

<p>If all you need to do is add some functionality to an existing library - perhaps add a function or two - then
it's overkill to replace the entire library with your version.  In this case it's better to simply extend the class.
Extending a class is nearly identical to replacing a class with a couple exceptions:</p>

<li>The class declaration must extend the parent class.</li>
<li>Your new class name and filename must be prefixed with <kbd>MY_</kbd> (this item is configurable.  See below.).</li>

<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>

class MY_Email extends CI_Email {<br /><br />


<p>Note: If you need to use a constructor in your class make sure you extend the parent constructor:</p>

class MY_Email extends CI_Email {<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;public function __construct()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::__construct();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />

<h3>Loading Your Sub-class</h3>

<p>To load your sub-class you'll use the standard syntax normally used.  DO NOT include your prefix.  For example,
to load the example above, which extends the Email class, you will use:</p>


<p>Once loaded you will use the class variable as you normally would for the class you are extending.  In the case of
the email class all calls will use:</p>


<h3>Setting Your Own Prefix</h3>

<p>To set your own sub-class prefix, open your <dfn>application/config/config.php</dfn> file and look for this item:</p>

<code>$config['subclass_prefix'] = 'MY_';</code>

<p>Please note that all native CodeIgniter libraries are prefixed with <kbd>CI_</kbd> so DO NOT use that as your prefix.</p>

<!-- END CONTENT -->

<div id="footer">
Previous Topic:&nbsp;&nbsp;<a href="libraries.html">Using CodeIgniter Libraries</a>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="drivers.html">Using CodeIgniter Drivers</a>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2011 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>