summaryrefslogtreecommitdiffstats
path: root/user_guide
diff options
context:
space:
mode:
authorDerek Jones <derek.jones@ellislab.com>2008-01-28 22:00:20 +0100
committerDerek Jones <derek.jones@ellislab.com>2008-01-28 22:00:20 +0100
commit269b942a2bf7b022795e591d9b0ad04526ee7e09 (patch)
treef465bb5a700d4cc5d72ca6e55e251640a46b869b /user_guide
parenta25530f6594c7ba45b3faa9537fda9f807069759 (diff)
added ability to "extend" helpers
* modified Loader to check for prefixed helpers in application/helpers folder * surrounded provided helper functions with if (! function_exists('foo')) conditionals so the user's helper functions take precedent.
Diffstat (limited to 'user_guide')
-rw-r--r--user_guide/changelog.html1
-rw-r--r--user_guide/general/helpers.html45
2 files changed, 46 insertions, 0 deletions
diff --git a/user_guide/changelog.html b/user_guide/changelog.html
index d8a67405e..d029f3222 100644
--- a/user_guide/changelog.html
+++ b/user_guide/changelog.html
@@ -129,6 +129,7 @@ Change Log
<li>Helpers &amp; Plugins
<ul>
+ <li>Added ability to <a href="./general/helpers.html">"extend" Helpers</a>.</li>
<li>Added an <a href="./helpers/email_helper.html">email helper</a> into core helpers.</li>
<li>Added <kbd>strip_quotes()</kbd> function to <a href="./helpers/string_helper.html">string helper</a>.</li>
<li>Added <kbd>reduce_multiples()</kbd> function to <a href="./helpers/string_helper.html">string helper</a>.</li>
diff --git a/user_guide/general/helpers.html b/user_guide/general/helpers.html
index 0b83884b5..38ce76815 100644
--- a/user_guide/general/helpers.html
+++ b/user_guide/general/helpers.html
@@ -116,6 +116,51 @@ This is done by opening the <var>application/config/autoload.php</var> file and
<p>Where "Click Here" is the name of the link, and "blog/comments" is the URI to the controller/function you wish to link to.</p>
+<h2>"Extending" Helpers</h2>
+
+<p>To "extend" Helpers, create a file in your <dfn>application/helpers/</dfn> folder with an identical name to the existing Helper, but prefixed with <kbd>MY_</kbd> (this item is configurable. See below.).</p>
+
+<p>If all you need to do is add some functionality to an existing helper - perhaps add a function or two, or change how a particular
+ helper function operates - then it's overkill to replace the entire helper with your version. In this case it's better to simply
+ "extend" the Helper. The term "extend" is used loosely since Helper functions are procedural and discrete and cannot be extended
+ in the traditional programmatic sense. Under the hood, this gives you the ability to add to the functions a Helper provides,
+ or to modify how the native Helper functions operate.</p>
+
+<p>For example, to extend the native <kbd>Array Helper</kbd> you'll create a file named <dfn>application/helpers/</dfn><kbd>MY_array_helper.php</kbd>, and add or override functions:</p>
+
+<code>
+// any_in_array() is not in the Array Helper, so it defines a new function<br />
+function any_in_array($needle, $haystack)<br />
+{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;$needle = (is_array($needle)) ? $needle : array($needle);<br />
+ <br />
+&nbsp;&nbsp;&nbsp;&nbsp;foreach ($needle as $item)<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (in_array($item, $haystack))<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TRUE;<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+ <br />
+&nbsp;&nbsp;&nbsp;&nbsp;return FALSE;<br />
+}<br />
+<br />
+// random_element() is included in Array Helper, so it overrides the native function<br />
+function random_element($array)<br />
+{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;shuffle($array);<br />
+&nbsp;&nbsp;&nbsp;&nbsp;return array_pop();<br />
+}<br />
+</code>
+
+<h3>Setting Your Own Prefix</h3>
+
+<p>The filename prefix for "extending" Helpers is the same used to extend libraries and Core classes. To set your own 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>
+
<h2>Now What?</h2>