Welcome Guest, Not a member yet? Register   Sign In
Getting object reference from load->library
#1

[eluser]WillKemp[/eluser]
Hi,

$this->load->libary() doesn't appear to return a reference to the object it instantiates, in the way that 'new' does. I know i can set the object name and refer to it that way, but that doesn't seem to be any use when the object name is dyamic (i.e., i need to instantiate an unknown number of objects in a loop). I need to pass the object references to a view.

I'm having to load the class using 'include()' and then create the objects using 'new' - which works fine. But it breaks consistency and i'd rather do it in a codeigniter sort of way if possible.

Is there any way to get a reference to a 'load'ed object in this sort of context?
#2

[eluser]jhabbley[/eluser]
I'm relatively new to CI, but my understanding is that you would do as follows:

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

$this->foo->some_method();
$property = $this->foo->some_property;

alternatively, you can instantiate your library with a specific name, ie:

Code:
$this->load->library('foo', NULL, 'bar');
$this->bar->some_method();

(Edit: missed the 2nd parameter (NULL) to the library loader method, which is intended for a parameter array)
#3

[eluser]WillKemp[/eluser]
Thanks. But none of those give me a reference that i can pass from the controller to the view.

The problem is that i can't use a specific name because there can be zero or more objects instantiated at the same time.
#4

[eluser]WillKemp[/eluser]
[quote author="WillKemp" date="1238717002"]Thanks. But none of those give me a reference that i can pass from the controller to the view.[/quote]

Well, that's not quite true - the "$property" could be passed, but i'd need to pass more than one per object and i'd rather pass the object itself as it seems less messy.
#5

[eluser]jhabbley[/eluser]
Well, my point is that $this->foo *IS* the reference that you could pass to the view.
#6

[eluser]WillKemp[/eluser]
[quote author="jhabbley" date="1238717662"]Well, my point is that $this->foo *IS* the reference that you could pass to the view.[/quote]

Yeah, but if i don't know what the "foo" part of $this->foo is, it gets a bit tricky. I can't use $this->'blah'.$i , for example.
#7

[eluser]jhabbley[/eluser]
you do know what foo is when you load the library.

Code:
$this->load->library('email');
$data = array('email_object' => $this->email);
$this->parser->parse('someview', $data);

"$this->email" is the object instance of the email library that you loaded.
#8

[eluser]WillKemp[/eluser]
[quote author="jhabbley" date="1238722112"]you do know what foo is when you load the library.

Code:
$this->load->library('email');
$data = array('email_object' => $this->email);
$this->parser->parse('someview', $data);

"$this->email" is the object instance of the email library that you loaded.[/quote]

Yeah, of course. Sorry, i was getting confused there. I need to have several instances of the object at the same time, so i'd need to use the third parameter to $this->load->library() to give each object a different name. It's that name i don't know.

e.g.,

Code:
include( 'Aliasformfield.php' );
$aliasObjs = array();
$aliases = $this->dbfetch->aliases( $id );
$i = 0;
foreach ( $aliases as $alias )
{
    $params = array(
        'id' => $alias->aliasId
        , 'forename' => $alias->forename
        , 'surname' => $alias->surname
        );
    $aliasObjs[$i] = new aliasformfield( $params );
    $i++;
}

How can i do that using $this->load->library() instead of 'new'?
#9

[eluser]jhabbley[/eluser]
My apologies, I now see that you're dynamically loading a library (of which you only know the name of at runtime). I apparently missed that part of your first post.

When you load each of the libraries in the loop, you can assign a custom name to the objects as you instantiate them (using the 3 argument method of the library loader.

Code:
for($i=0; $i<count($libraries); $i++) {
  $handle = 'library_'.$i
  $this->load->library($libraries[i], NULL, $handle);
  $data[$handle] = $this->$handle;
}

But yeah, that gets kind of ugly...
#10

[eluser]WillKemp[/eluser]
Ah. Yeah. It's really obvious now you point it out! I must have been staring at this screen for a few too many hours today! ;-)

And, yeah, it is a bit ugly, but i think it's better than using 'include' and 'new' in a codeigniter environment.

Thanks for your help and your patience!




Theme © iAndrew 2016 - Forum software by © MyBB