From 33de9a144aad28763405f8ae2d5c59df5e929b4f Mon Sep 17 00:00:00 2001
From: admin 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 directory in order to maintain separation between your local resources and the global framework resources.Creating Libraries
As an added bonus, Code Igniter 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:
+ +Your library classes should be placed within your application/libraries folder, as this is where Code Igniter will look for them when -they are initialized. If your class is named identically to a native class from the system/libraries folder, your version -will be used instead.
+they are initialized.Classes should have this basic prototype (Note: We are using the name Myclass purely as an example):
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
-
-// Initialize the class
-$obj =& get_instance();
-$obj->init_class('Myclass');
class Myclass {
@@ -101,15 +110,6 @@ class Myclass {
?>
-You'll notice in the above example that the class is instantiated directly from the file itself using these two lines of code:
- -$obj =& get_instance();
-$obj->init_class('Myclass');
-
-Make sure and submit your class name in the first parameter of the $obj->init_class() function. In the -above example it is Myclass
- -From within any of your Controller functions you can initialize your class using the standard:
@@ -124,22 +124,6 @@ Code Igniter doesn't care.$this->myclass->some_function(); // Object instances will always be lower case
-If you would like the object variable ($this->myclass) set to a different name you can specify it when initializing your class. For -example, let's initialize it as foobar:
- -$obj =& get_instance();
-$obj->init_class('Myclass', 'foobar');
-
-In the above example you would still load your class like this:
- -$this->load->library('Mclass');
-
-But you would use it like this:
-
-$this->foobar->function();
You can also pass parameters via the third parameter of the $obj->init_class() function:
- -<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
-
-
-$params = array('type' => 'large', 'color' => 'red');
-
-// Initialize the class
-$obj =& get_instance();
-$obj->init_class('Myclass', 'myclass', $params);
-
-class Myclass {
-
- function Myclass($params)
- {
- // Do something with $params
- }
-}
-?>
+You can also pass parameters stored in a config file. Simply create a config file named identically to the class filename.
-- cgit v1.2.3-24-g4f1b