From ebbe383453f3f18d09a8c7f3437d5712bdb8e650 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 26 Sep 2006 06:02:47 +0000 Subject: --- user_guide/general/creating_libraries.html | 144 ++++++++++++++++++----------- 1 file changed, 91 insertions(+), 53 deletions(-) (limited to 'user_guide/general') diff --git a/user_guide/general/creating_libraries.html b/user_guide/general/creating_libraries.html index da096d890..dba5f49e8 100644 --- a/user_guide/general/creating_libraries.html +++ b/user_guide/general/creating_libraries.html @@ -68,93 +68,131 @@ your application directory in order to maintain separation between yo

Storage

-

In order for your libraries to be stored in your application folder you will need to create two directories in which to store them:

+

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.

- - - -

Anatomy of a Library

-

A class library consists of two components:

- -
    -
  1. An init file.
  2. -
  3. A class file.
  4. -
- -

The Init File

+

Naming Conventions

-

An init file a small initialization file corresponding to each of your classes. The purpose of this file is to -instantiate a particular class. Each init file must be named identically to your class file name, adding the "init_" prefix. For example, if your -class is named myclass.php your init file will be named:

+ -init_myclass.php -

Within your init file you will place your initialization code. Here's an example of such code, using an imaginary class named Myclass:

+

The Class File

+

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');

-if ( ! class_exists('Myclass'))
-{
-     require_once(APPPATH.'libraries/Myclass'.EXT);
-}
-
+// Initialize the class
$obj =& get_instance();
-$obj->myclass = new Myclass();
+$obj->init_class('Myclass'); +

+class Myclass {

+    function some_function()
+    {
+    }
+}

?>
-

The Class File

-

Your class file itself will be placed inside your libraries directory:

+

You'll notice in the above example that the class is instantiated directly from the file itself using these two lines of code:

-application/libraries/myclass.php +$obj =& get_instance();
+$obj->init_class('Myclass');
-

The class will have this basic prototype:

+

Make sure and submit your class name in the first parameter of the $obj->init_class() function. In the +above example it is Myclass

-<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
-
-class Myclass {
-
-}
-?>
+

Using Your Class

-

Naming Conventions

+

From within any of your Controller functions you can initialize your class using the standard:

- +$this->load->library('Mclass'); +

Where Myclass is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case. +Code Igniter doesn't care.

-

Using Your Class

+

Once loaded you can access your class using:

-

From within any of your Controller classes you can initialize your class using the standard:

+$this->myclass->some_function();  // Object instances will always be lower case + -$this->load->library('myclass'); +

Setting a Different Class Variable Name

-

Once loaded you can access your class using:

-$this->myclass->function(); +

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');
-

Note: In your init file you can define the object variable name.

+

In the above example you would still load your class like this:

+$this->load->library('Mclass'); -

Passing Parameters Your Class

+

But you would use it like this:

-

In the library loading function you can pass data via the second parameter and it will be available to your initialization file:

+$this->foobar->function(); + + +

Passing Parameters When Initializing Your Class

+ +

In the library loading function you can dynamically pass data via the second parameter and it will be passed to your class +constructor:

$params = array('type' => 'large', 'color' => 'red');

-$this->load->library('myclass', $params);
+$this->load->library('Myclass', $params); + +

If you use this feature you must set up your class constructor to expect data:

+ +<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+
+// Initialize the class
+$obj =& get_instance();
+$obj->init_class('Myclass'); +

+class Myclass {
+
+    function Myclass($params)
+    {
+        // Do something with $params
+    }
+}

+?>
+ +

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
+    }
+}

+?>
+ + + + -

Parameters will be accessible using a variable called $params. By default this variable is set to FALSE.

Utilizing Code Igniter Resources within Your Library

@@ -163,7 +201,7 @@ $this->load->library('myclass', $params);

To access Code Igniter's native resources within your library use the get_instance() function. This function returns the Code Igniter super object.

-

Normally, to call any of the available Code Igniter functions requires you to use the $this construct:

+

Normally from within your controller functions you will call any of the available Code Igniter functions using the $this construct:

$this->load->helper('url');
-- cgit v1.2.3-24-g4f1b