php - How can I make a library globally accessible? - Stack Overflow

admin2025-04-20  3

I have a library with lot of functions which I need all around the controllers, helper, models etc.

Also I want to reduce the code inside each part of them.

So I try to init the library once and have it available globally.

To include it into the basecontroller/basemodel failed, I only find the normal way about namespace and get the library by "$mylib=new Mylib"();", this both rows of code I don't to avoid. Also it will be nice, if its the same instance to reduce resources and not get every time a new instance for the same functions...

I look around about factories and services, but I don't understand the way like both works, also I couldn't find a simple complete example from including, define function and call them with params, so I think services or factories can be a way for it?

So is there a way to make a library globally available?

I have a library with lot of functions which I need all around the controllers, helper, models etc.

Also I want to reduce the code inside each part of them.

So I try to init the library once and have it available globally.

To include it into the basecontroller/basemodel failed, I only find the normal way about namespace and get the library by "$mylib=new Mylib"();", this both rows of code I don't to avoid. Also it will be nice, if its the same instance to reduce resources and not get every time a new instance for the same functions...

I look around about factories and services, but I don't understand the way like both works, also I couldn't find a simple complete example from including, define function and call them with params, so I think services or factories can be a way for it?

So is there a way to make a library globally available?

Share Improve this question edited Feb 11 at 6:20 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 24 at 19:00 relo80relo80 315 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

In the meantime i've found a way to make my library avivable in controllers, models, helpers and views without creating a new instance of them.

the follwing way works for me

  1. base_controller on initController i create my lib-instance "$this->mylib = new mylib();"

  2. to make them aviable in other controllers i need to add take care to have "use App\Controllers\BaseController;" and add "use App\Libraries\Mylib;" now in the controller with "$this->mylib->myfunction()" the lib-instance is avivable.

  3. to make it also avivable in views etc. inspired by this post: Codeigniter 4 - get instance in helper function so a) $CI_INSTANCE = []; # It keeps a ref to global CI instance function register_ci_instance(\App\Controllers\BaseController &$_ci) { global $CI_INSTANCE; $CI_INSTANCE[0] = &$_ci; } function get_instance(): \App\Controllers\BaseController { global $CI_INSTANCE; return $CI_INSTANCE[0]; }

b) go back to basecontroller and add in the initcontroller part

register_ci_instance($this);

c) Now with

$ci = &get_instance();      
$ci->mylib->myfunction();

is avivable.

For me it works, hope it helps.

You would like a globally available instance of your library. For this to affect things like helpers / models / libraries / controllers I would modify the index.php file found in htdocs and add something like:

include('/var/www/html/your_path/libraries/MyLibrary.php');
define('MYLIBRARY',new MyLibrary);

Then in your code you can call it using a MYLIBRARY constant. Depending on when your library is loaded you may be able to refer to APPPATH instead of the full path in the include.

Looking at the code, you are confusing version CI 4 and CI 3. In the latest version, there is no need for get_instance()

For v4.6, you can create a service in app/Config/Services.php that will be available after initializing the application.

https://codeigniter4.github.io/userguide/concepts/services.html#defining-services

If you need an earlier creation of an object, you can place it in app/Common.php or in app/Config/Events.php.

https://codeigniter4.github.io/userguide/extending/events.html#defining-an-event

转载请注明原文地址:http://anycun.com/QandA/1745120541a90382.html