From 0625e19f3318874d6f95b546312601538edb0f14 Mon Sep 17 00:00:00 2001
From: admin 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 Core Class
Extending a class is identical to replacing a class with one exception: The class declaration must extend the parent class -and your new class must be prefixed with MY_. For example, to extend the native Input class -you'll create a file named application/libraries/Input.php, and declare your class with:
+For example, to extend the native Input class you'll create a file named application/libraries/MY_Input.php, and declare your class with:
class MY_Input extends CI_Input {
@@ -135,7 +139,8 @@ class MY_Input extends CI_Input {
}
}
-Any functions in your class that are named identically to the functions in the parent class will be used instead of the native ones. +
Tip: Any functions in your class that are named identically to the functions in the parent class will be used instead of the native ones +(this is known as "method overloading"). This allows you to substantially alter the Code Igniter core.
diff --git a/user_guide/general/creating_libraries.html b/user_guide/general/creating_libraries.html index 3f168909d..d76464120 100644 --- a/user_guide/general/creating_libraries.html +++ b/user_guide/general/creating_libraries.html @@ -226,17 +226,24 @@ class CI_Email {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.
+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: -Extending a class is identical to replacing a class with one exception: The class declaration must extend the parent class -and your new class must be prefixed with MY_. For example, to extend the native Email class -you'll create a file named application/libraries/Email.php, and declare your class with:
+For example, to extend the native Email class you'll create a file named application/libraries/MY_Email.php, and declare your class with:
class MY_Email extends CI_Email {
}
+Note: If you need to use a constructor in your class make sure you extend the parent constructor:
+ +
class MY_Email extends CI_Email {
@@ -246,9 +253,13 @@ class MY_Email extends CI_Email {
}
}
-Important: To tell Code Igniter to load your sub-class you MUST include my_ in the loading function:
-$this->load->library('my_email');
+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:
+ +$this->load->library('email');
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:
@@ -256,6 +267,17 @@ the email class all calls will use:
$this->email->some_function();
+
+
To set your own sub-class prefix, open your application/config/config.php file and look for this item:
+ +$config['subclass_prefix'] = 'MY_';
+
+Please note that all native Code Igniter libraries are prefixed with CI_ so DO NOT use that as your prefix.
+ + + -- cgit v1.2.3-24-g4f1b