CodeIgniter Forums
passing data to a custom library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: passing data to a custom library (/thread-34176.html)



passing data to a custom library - El Forum - 09-21-2010

[eluser]skaterdav85[/eluser]
I have some pre-built classes, and so i added them to my libraries folder under application. I noticed that you have to pass in an array when you load the library if you want to pass any data to the contstructor. What if the constructor accepts only Strings, not arrays? How would I load the library with my string?

This is my code in my controller that doesnt work:

Code:
$url = "http://tinygeocoder.com/create-api.php?q=Perris,CA";
$this->load->library('RemoteConnector', $url);



passing data to a custom library - El Forum - 09-21-2010

[eluser]pickupman[/eluser]
You should be able to use
Code:
$this->load->library('RemoteConnector', array($url));

Or just edit the library so it will take an array. Take a look at the Email.php file in /system/libraries/Email.php.

An array is passed to the constructor. If the array exists, it calls the initialize() method. That method loops through each array key=>value pair and sets the classes property=>value.

The array is used to order the arguments for the constructor. CI doesn't know how many values you need to pass.


passing data to a custom library - El Forum - 09-21-2010

[eluser]fesweb[/eluser]
[quote author="skaterdav85" date="1285133410"]
Code:
$url = "http://tinygeocoder.com/create-api.php?q=Perris,CA";
$this->load->library('RemoteConnector', $url);
[/quote]
This is my approach, cribbed from some CI libraries.
Library code:
Code:
// set a bunch of vars for use throughout the library
class Courses_lib {
    var $url    = FALSE;
    var $more_info     = array(
                    'id' => 0,
                    'text' => FALSE,
                    'description' => ''
                );    
// use the constructor to run the initialization function
// pass an array, or nothing, when you load the library
    function __construct($params = array())
    {
        if (count($params) > 0)
        {
            $this->initialize($params);        
        }
    }
// any item in the array can be a string, or an array
// any items passed in the array will overwrite the default var
// all other vars will contain their original value
    function initialize($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = trim($val);
                }
            }
        }
    }
// simple method
        function link_link()
        {
            if($this->url != FALSE)
            {
                $output = '<a >url.'">Link</a>';
            }
            else
            {
                $output = 'No URL provided';
             }
             return $output;
        }
// similar, but different method
        function url_link()
        {
            if($this->url != FALSE)
            {
                $output = '<a >url.'">'.$this->url.'</a>';
            }
            else
            {
                $output = 'No URL provided';
             }
             return $output;
        }
In the controller, you can load the library with or without passing params, and you can the re-initialize within the same controller.
Code:
$this->load->library('courses_lib');
echo $this->courses_lib->link_link(); // outputs "No URL provided"

// you can re-initialize and run the method again
$data['url'] = 'http://www.whatever.com';
$this->courses_lib->initialize($data);
echo $this->courses_lib->link_link();
// outputs "<a href="http://www.whatever.com">Link</a>
echo $this->courses_lib->url_link();
// outputs "<a href="http://www.whatever.com">http://www.whatever.com</a>



passing data to a custom library - El Forum - 09-22-2010

[eluser]danmontgomery[/eluser]
(this is covered in the user guide)


passing data to a custom library - El Forum - 10-08-2010

[eluser]skaterdav85[/eluser]
thanks for the help. i was hesitant to modify the library just because i know it isn't good practice to edit a pre-tested class. however this seemed like the easiest solution so i just modified the constructor with one line to accept that particular index in the array passed, like this:

Code:
before
$this->_url = $url;

my changes:
$this->_url = $url['url'];



passing data to a custom library - El Forum - 08-05-2011

[eluser]mikedfunk[/eluser]
I did it this way:

Code:
if (is_array($param)) {$param = $param[0];}

then I can just pass in an array:

Code:
$this->load->library('lib_name', array('value'));



passing data to a custom library - El Forum - 08-05-2011

[eluser]Aken[/eluser]
Unfortunately, CI developers have limited the parameter field of the library loader to only arrays out of the box. However, you can extend the Loader core class and replace the Library function with a slight modification to accept any variable. Be careful in doing so, though, as some libraries may not have any sort of handling/sanitization of their parameter field, and expect to only accept arrays. You want to make sure to always pass the exact type of parameter expected when loading a library.

Otherwise, you can just modify your library constructors to handle arrays, like fesweb showed (that's the way I prefer to do it).