From 269b942a2bf7b022795e591d9b0ad04526ee7e09 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Mon, 28 Jan 2008 21:00:20 +0000 Subject: 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. --- user_guide/general/helpers.html | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'user_guide/general/helpers.html') 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 application/config/autoload.php file and

Where "Click Here" is the name of the link, and "blog/comments" is the URI to the controller/function you wish to link to.

+

"Extending" Helpers

+ +

To "extend" Helpers, create a file in your application/helpers/ folder with an identical name to the existing Helper, but prefixed with MY_ (this item is configurable. See below.).

+ +

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.

+ +

For example, to extend the native Array Helper you'll create a file named application/helpers/MY_array_helper.php, and add or override functions:

+ + +// any_in_array() is not in the Array Helper, so it defines a new function
+function any_in_array($needle, $haystack)
+{
+    $needle = (is_array($needle)) ? $needle : array($needle);
+
+    foreach ($needle as $item)
+    {
+        if (in_array($item, $haystack))
+        {
+            return TRUE;
+        }
+        }
+
+    return FALSE;
+}
+
+// random_element() is included in Array Helper, so it overrides the native function
+function random_element($array)
+{
+    shuffle($array);
+    return array_pop();
+}
+
+ +

Setting Your Own Prefix

+ +

The filename prefix for "extending" Helpers is the same used to extend libraries and Core classes. To set your own prefix, open your application/config/config.php file and look for this item:

+ +$config['subclass_prefix'] = 'MY_'; + +

Please note that all native CodeIgniter libraries are prefixed with CI_ so DO NOT use that as your prefix.

+

Now What?

-- cgit v1.2.3-24-g4f1b