Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter correct way to check if libraries/helper/core files is loaded

i'm using codeigniter 2.

Appreciate if someone could show the correct way to check if the following files :
- library file is loaded?
- helper file is loaded?
- config file is loaded?
- model file is loaded?
- third_party file is loaded?

regards

like image 704
user1884324 Avatar asked May 14 '13 05:05

user1884324


2 Answers

You can use the native PHP function class_exists() to determine if the class has been defined, before calling it. In same regard, using method_exists() will check if class method exists.

Since helpers are collection of function, not methods, checking can be done using function_exists().

if (class_exists('Library')) 
{
    $this->library->myMethod();
}

For further info please refer to

http://php.net/manual/en/function.class-exists.php.

http://us.php.net/manual/en/function.method-exists.php

like image 176
Marko Aleksić Avatar answered Oct 21 '22 22:10

Marko Aleksić


You don't have to check, you just load them wherever you need to be sure to have them.

Using CI's Load library ($this->load->[library|model|helper]) will always load it just once. You can see this if you turn on your debug logging.

like image 28
minboost Avatar answered Oct 21 '22 23:10

minboost