diff options
author | Derek Jones <derek.jones@ellislab.com> | 2008-01-28 22:00:20 +0100 |
---|---|---|
committer | Derek Jones <derek.jones@ellislab.com> | 2008-01-28 22:00:20 +0100 |
commit | 269b942a2bf7b022795e591d9b0ad04526ee7e09 (patch) | |
tree | f465bb5a700d4cc5d72ca6e55e251640a46b869b /user_guide/general/helpers.html | |
parent | a25530f6594c7ba45b3faa9537fda9f807069759 (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/general/helpers.html')
-rw-r--r-- | user_guide/general/helpers.html | 45 |
1 files changed, 45 insertions, 0 deletions
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 />
+ $needle = (is_array($needle)) ? $needle : array($needle);<br />
+ <br />
+ foreach ($needle as $item)<br />
+ {<br />
+ if (in_array($item, $haystack))<br />
+ {<br />
+ return TRUE;<br />
+ }<br />
+ }<br />
+ <br />
+ 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 />
+ shuffle($array);<br />
+ 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>
|