CodeIgniter Forums
Loading library from name - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Loading library from name (/showthread.php?tid=62437)



Loading library from name - yad - 07-14-2015

Hi,

I have trouble to use a library that I load depending on a variable data.

$mylibrary = 'Custom_Lib';
$mymethod = 'Method1';
//those two are retreived from a database.

//After I use the method:

$this->load->library($mylibrary);
$this->$mylibrary->$method();

//I have an error that I'm calling a method on a non object...

I've tried also
class_alias(strtolower($mylibrary), 'useclass');
$this->useclass->$method($record);

//same problem...

Any one have an advise to call a special library/method from dynamicly ?

Thanks for your help,

Yannick


RE: Loading library from name - CroNiX - 07-14-2015

Does it work if you access it directly instead of using a variable?

One thing that may cause that is when you use $this->library_name, "library_name" I believe should be lower case. You're using $mylibrary, where the variables value isn't lower case.

So it just might need something like:
$this->load->library($mylibrary);
$mylibrary = strtolower($mylibrary); //force to lower case before accessing
$this->$mylibrary->$method();

Did you try it using the actual names instead of variables to test?


RE: Loading library from name - yad - 07-14-2015

Thanks CroNIX,

Your solution was the right one !

Always use lowercase for library call...