From e1bf94291750a592b1a9749240f08a3d5edb7d39 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 28 Sep 2006 07:15:05 +0000 Subject: --- user_guide/general/creating_libraries.html | 75 +++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 16 deletions(-) (limited to 'user_guide/general/creating_libraries.html') diff --git a/user_guide/general/creating_libraries.html b/user_guide/general/creating_libraries.html index a2d40f95d..d84cf2e23 100644 --- a/user_guide/general/creating_libraries.html +++ b/user_guide/general/creating_libraries.html @@ -78,7 +78,7 @@ to an existing library. Or you can even replace native libraries just by placing
  • You can replace native libraries.
  • - +

    The page below explains these three concepts in detail.

    Storage

    @@ -97,11 +97,11 @@ they are initialized.

    The Class File

    -

    Classes should have this basic prototype (Note: We are using the name Myclass purely as an example):

    +

    Classes should have this basic prototype (Note: We are using the name Someclass purely as an example):

    -<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    +<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    -class Myclass {
    +class Someclass {

        function some_function()
        {
    @@ -114,14 +114,14 @@ class Myclass {

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

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

    Where Myclass is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case. +

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

    -

    Once loaded you can access your class using:

    +

    Once loaded you can access your class using the lower case version:

    -$this->myclass->some_function();  // Object instances will always be lower case +$this->someclass->some_function();  // Object instances will always be lower case @@ -134,26 +134,24 @@ constructor:

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

    -$this->load->library('Myclass', $params);
    +$this->load->library('Someclass', $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 {
    +class Someclass {

    -    function Myclass($params)
    +    function Someclass($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.

    +

    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.

    @@ -201,8 +199,53 @@ etc. This is very important. Assigning by reference allows you to use the original Code Igniter 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 Code Igniter 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'); + + + +

    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 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:

    + + +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 {
    +
    +    function My_Email()
    +    {
    +        parent::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'); -- cgit v1.2.3-24-g4f1b