From 8ede1a2ecbb62577afd32996956c5feaf7ddf9b6 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 5 Oct 2011 13:34:52 -0500 Subject: replacing the old HTML user guide with a Sphinx-managed user guide --- .../source/general/creating_libraries.rst | 187 +++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 user_guide_src/source/general/creating_libraries.rst (limited to 'user_guide_src/source/general/creating_libraries.rst') diff --git a/user_guide_src/source/general/creating_libraries.rst b/user_guide_src/source/general/creating_libraries.rst new file mode 100644 index 000000000..d322f56eb --- /dev/null +++ b/user_guide_src/source/general/creating_libraries.rst @@ -0,0 +1,187 @@ +################## +Creating Libraries +################## + +When we use the term "Libraries" we are normally referring to the +classes that are located in the libraries directory and described in the +Class Reference of this user guide. In this case, however, we will +instead describe how you can create your own libraries within your +application/libraries directory in order to maintain separation between +your local resources and the global framework resources. + +As an added bonus, CodeIgniter permits your libraries to extend native +classes if you simply need to add some functionality to an existing +library. Or you can even replace native libraries just by placing +identically named versions in your application/libraries folder. + +In summary: + +- You can create entirely new libraries. +- You can extend native libraries. +- You can replace native libraries. + +The page below explains these three concepts in detail. + +.. note:: The Database classes can not be extended or replaced with your + own classes. All other classes are able to be replaced/extended. + +Storage +======= + +Your library classes should be placed within your application/libraries +folder, as this is where CodeIgniter will look for them when they are +initialized. + +Naming Conventions +================== + +- File names must be capitalized. For example: Myclass.php +- Class declarations must be capitalized. For example: class Myclass +- Class names and file names must match. + +The Class File +============== + +Classes should have this basic prototype (Note: We are using the name +Someclass purely as an example):: + + ` functions you +can initialize your class using the standard:: + + $this->load->library('someclass'); + +Where *someclass* is the file name, without the ".php" file extension. +You can submit the file name capitalized or lower case. CodeIgniter +doesn't care. + +Once loaded you can access your class using the lower case version:: + + $this->someclass->some_function();  // Object instances will always be lower case + +Passing Parameters When Initializing Your Class +=============================================== + +In the library loading function you can dynamically pass data as an +array via the second parameter and it will be passed to your class +constructor:: + + $params = array('type' => 'large', 'color' => 'red'); $this->load->library('Someclass', $params); + +If you use this feature you must set up your class constructor to expect +data:: + + + +You can also pass parameters stored in a config file. Simply create a +config file named identically to the class file name and store it in +your application/config/ folder. Note that if you dynamically pass +parameters as described above, the config file option will not be +available. + +Utilizing CodeIgniter Resources within Your Library +=================================================== + +To access CodeIgniter's native resources within your library use the +get_instance() function. This function returns the CodeIgniter super +object. + +Normally from within your controller functions you will call any of the +available CodeIgniter functions using the $this construct:: + + $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); etc. + +$this, however, only works directly within your controllers, your +models, or your views. If you would like to use CodeIgniter's classes +from within your own custom classes you can do so as follows: + +First, assign the CodeIgniter object to a variable:: + + $CI =& get_instance(); + +Once you've assigned the object to a variable, you'll use that variable +*instead* of $this:: + + $CI =& get_instance(); $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc. + +.. note:: You'll notice that the above get_instance() function is being + passed by reference:: + + $CI =& get_instance(); + + This is very important. Assigning by reference allows you to use the + original CodeIgniter object rather than creating a copy of it. + +Replacing Native Libraries with Your Versions +============================================= + +Simply by naming your class files identically to a native library will +cause CodeIgniter to use it instead of the native one. To use this +feature you must name the file and the class declaration exactly the +same as the native library. For example, to replace the native Email +library you'll create a file named application/libraries/Email.php, and +declare your class with:: + + class CI_Email { } + +Note that most native classes are prefixed with CI\_. + +To load your library you'll see the standard loading function:: + + $this->load->library('email'); + +.. note:: At this time the Database classes can not be replaced with + your own versions. + +Extending Native Libraries +========================== + +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 a class is nearly identical to replacing a class with a +couple exceptions: + +- The class declaration must extend the parent class. +- Your new class name and filename must be prefixed with MY\_ (this + item is configurable. See below.). + +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 {     public function __construct()     {         parent::__construct();     } } + +Loading Your Sub-class +---------------------- + +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:: + + $this->email->some_function(); + +Setting Your Own Prefix +----------------------- + +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 CodeIgniter libraries are prefixed with CI\_ +so DO NOT use that as your prefix. -- cgit v1.2.3-24-g4f1b From 9713cce9876fce5cb38c3ee24193ae3455c81755 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 5 Oct 2011 17:26:43 -0500 Subject: fixing code spacing in Core, Library, Drivers, and Errors general docs --- .../source/general/creating_libraries.rst | 55 ++++++++++++++++++---- 1 file changed, 47 insertions(+), 8 deletions(-) (limited to 'user_guide_src/source/general/creating_libraries.rst') diff --git a/user_guide_src/source/general/creating_libraries.rst b/user_guide_src/source/general/creating_libraries.rst index d322f56eb..bc545b483 100644 --- a/user_guide_src/source/general/creating_libraries.rst +++ b/user_guide_src/source/general/creating_libraries.rst @@ -45,7 +45,16 @@ The Class File Classes should have this basic prototype (Note: We are using the name Someclass purely as an example):: - 'large', 'color' => 'red'); $this->load->library('Someclass', $params); + $params = array('type' => 'large', 'color' => 'red'); + + $this->load->library('Someclass', $params); If you use this feature you must set up your class constructor to expect data:: - + You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in @@ -93,7 +114,10 @@ object. Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct:: - $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); etc. + $this->load->helper('url'); + $this->load->library('session'); + $this->config->item('base_url'); + // etc. $this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes @@ -106,7 +130,12 @@ First, assign the CodeIgniter object to a variable:: Once you've assigned the object to a variable, you'll use that variable *instead* of $this:: - $CI =& get_instance(); $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc. + $CI =& get_instance(); + + $CI->load->helper('url'); + $CI->load->library('session'); + $CI->config->item('base_url'); + // etc. .. note:: You'll notice that the above get_instance() function is being passed by reference:: @@ -126,7 +155,9 @@ same as the native library. For example, to replace the native Email library you'll create a file named application/libraries/Email.php, and declare your class with:: - class CI_Email { } + class CI_Email { + + } Note that most native classes are prefixed with CI\_. @@ -153,12 +184,20 @@ couple exceptions: 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 { } + 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 {     public function __construct()     {         parent::__construct();     } } + class MY_Email extends CI_Email { + + public function __construct() + { + parent::__construct(); + } + } Loading Your Sub-class ---------------------- -- cgit v1.2.3-24-g4f1b From 1d571971be8be78a92d31aad27dda4009770043f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 7 Mar 2012 11:49:35 +0200 Subject: Update the example on extending libraries with a constructor --- user_guide_src/source/general/creating_libraries.rst | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'user_guide_src/source/general/creating_libraries.rst') diff --git a/user_guide_src/source/general/creating_libraries.rst b/user_guide_src/source/general/creating_libraries.rst index bc545b483..673fbd4bb 100644 --- a/user_guide_src/source/general/creating_libraries.rst +++ b/user_guide_src/source/general/creating_libraries.rst @@ -188,17 +188,23 @@ application/libraries/MY_Email.php, and declare your class with:: } -Note: If you need to use a constructor in your class make sure you +If you need to use a constructor in your class make sure you extend the parent constructor:: class MY_Email extends CI_Email { - public function __construct() - { - parent::__construct(); - } + public function __construct($config = array()) + { + parent::__construct($config); + } + } +.. note:: + Not all of the libraries have the same (or any) parameters + in their constructor. Take a look at the library that you're + extending first to see how it should be implemented. + Loading Your Sub-class ---------------------- -- cgit v1.2.3-24-g4f1b