Welcome Guest, Not a member yet? Register   Sign In
multiple instances of the same library
#1

[eluser]Unknown[/eluser]
hello, we need to have more instances of the same library. And we noticed that Library Function from the Loader Object does accept an object name parameter and we thought that placing different object names would create different instances of the same library, the problem is that only the first one is successfully created.

Code:
$this->CI->load->library('rest', array('server' => $this->CI->config->item('api1_url')), 'some_api1');
$this->CI->load->library('rest', array('server' => $this->CI->config->item('api2_url')), 'some_api2');

thnks!

g.
#2

[eluser]textnotspeech[/eluser]
You could just load the library and then create new instances in plain old PHP like so:

Code:
$this->load->library('rest');

$api1 = new Rest($config1);
$api2 = new Rest($config2);
#3

[eluser]mattalexx[/eluser]
[quote author="gansodesoya" date="1284793493"]And we noticed that Library Function from the Loader Object does accept an object name parameter[/quote]

Really? Mine does. (1.7.2)

system/libraries/Loader.php
Code:
/**
* Class Loader
*
* This function lets users load and instantiate classes.
* It is designed to be called from a user's app controllers.
*
* @access    public
* @param    string    the name of the class
* @param    mixed    the optional parameters
* @param    string    an optional object name
* @return    void
*/    
function library($library = '', $params = NULL, $object_name = NULL)
{
    // ...
In my controller:
Code:
$this->load->library('shop');
$this->load->library('shop', NULL, 'shop2');
var_dump(spl_object_hash($this->shop)); // string(32) "7c29bccdea241a1d687bf2bcd079d72d"
var_dump(spl_object_hash($this->shop)); // string(32) "7c29bccdea241a1d687bf2bcd079d72d"
var_dump(spl_object_hash($this->shop2)); // string(32) "9f39067c36930b2ec4ceb174f518a23e"
#4

[eluser]InsiteFX[/eluser]
Even Codeigniter 2.0 still allows for this!

InsiteFX
#5

[eluser]icomefromthenet[/eluser]
I always just use the new keyword, I find for me it saves confusion.




Theme © iAndrew 2016 - Forum software by © MyBB