blob: f4961618a3cd36bf3b2d1c5042f8e17eaebe9abd [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
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00006
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() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 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' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000026
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>
Derek Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</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">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Creating Ancillary Classes
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 Ancillary Classes</h1>
64
admine334c472006-10-21 19:44:22 +000065<p>In some cases you may want to develop classes that exist apart from your controllers but have the ability to
Derek Allardd2df9bc2007-04-15 17:41:17 +000066utilize all of CodeIgniter's resources. This is easily possible as you'll see.</p>
adminb0dd10f2006-08-25 17:25:49 +000067
68<h2>get_instance()</h2>
69
70
Derek Allardd2df9bc2007-04-15 17:41:17 +000071<p><strong>Any class that you instantiate within your controller functions can access CodeIgniter's native resources</strong> simply by using the <kbd>get_instance()</kbd> function.
72This function returns the main CodeIgniter object.</p>
adminb0dd10f2006-08-25 17:25:49 +000073
Derek Allardd2df9bc2007-04-15 17:41:17 +000074<p>Normally, to call any of the available CodeIgniter functions requires you to use the <kbd>$this</kbd> construct:</p>
adminb0dd10f2006-08-25 17:25:49 +000075
76<code>
77<strong>$this</strong>->load->helper('url');<br />
78<strong>$this</strong>->load->library('session');<br />
79<strong>$this</strong>->config->item('base_url');<br />
80etc.
81</code>
82
admine334c472006-10-21 19:44:22 +000083<p><kbd>$this</kbd>, however, only works within your controllers, your models, or your views.
Derek Allardd2df9bc2007-04-15 17:41:17 +000084If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:</p>
adminb0dd10f2006-08-25 17:25:49 +000085
86
Derek Allardd2df9bc2007-04-15 17:41:17 +000087<p>First, assign the CodeIgniter object to a variable:</p>
adminb0dd10f2006-08-25 17:25:49 +000088
admin88a8ad12006-10-07 03:16:32 +000089<code>$CI =& get_instance();</code>
adminb0dd10f2006-08-25 17:25:49 +000090
91<p>Once you've assigned the object to a variable, you'll use that variable <em>instead</em> of <kbd>$this</kbd>:</p>
92
93<code>
admin88a8ad12006-10-07 03:16:32 +000094$CI =& get_instance();<br /><br />
95$CI->load->helper('url');<br />
96$CI->load->library('session');<br />
97$CI->config->item('base_url');<br />
adminb0dd10f2006-08-25 17:25:49 +000098etc.
99</code>
100
101<p class="important"><strong>Note:</strong> You'll notice that the above get_instance() function is being passed by reference:
102<br /><br />
admin88a8ad12006-10-07 03:16:32 +0000103<var>$CI =& get_instance();</var>
adminb0dd10f2006-08-25 17:25:49 +0000104<br /><br />
Derek Allardd2df9bc2007-04-15 17:41:17 +0000105This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.</p>
adminb0dd10f2006-08-25 17:25:49 +0000106</div>
107<!-- END CONTENT -->
108
109
110<div id="footer">
111<p>
112Previous Topic:&nbsp;&nbsp;<a href="creating_libraries.html">Creating Core Libraries</a>
113&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
114<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
115<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
116Next Topic:&nbsp;&nbsp;<a href="autoloader.html">Auto-loading Resources</a>
117<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000118<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000119</div>
120
121</body>
122</html>