CodeIgniter Forums
get_instance function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: get_instance function (/thread-66367.html)



get_instance function - superior - 10-16-2016

Hello,

I'm currently exploring the new CodeIgniter 4, like the whole new idea so i'm preppin my code into the new version 4.
Saw the form_helper is not implemented at this moment, so i've used to one from version 3 for my coding.

Then I saw the "get_instance" is not available, did this changed or is there something we need to call in order to use this?

Thanks in advance


RE: get_instance function - sv3tli0 - 10-16-2016

Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.


RE: get_instance function - superior - 10-17-2016

(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.


Wow that's a huge improvement, thank you for your answer/knowledge!


RE: get_instance function - webdevron - 08-15-2019

(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.

Could you please share any documentations or example regarding this issue? What is the process to assess a LIBRARY within HELPER?

Thanks in advance.


RE: get_instance function - dave friend - 08-15-2019

(08-15-2019, 06:47 AM)webdevron Wrote:
(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.

Could you please share any documentations or example regarding this issue? What is the process to assess a LIBRARY within HELPER?

Thanks in advance.

If you examine a CIv4 helper source file you should find several examples. Here are a couple from url_helper.php

PHP Code:
$url = new \CodeIgniter\HTTP\URI($fullPath);
//or for a "service"
$config = \CodeIgniter\Config\Services::request()->config

In some cases, you might find it helpful to include a "use" directive in a helper. For instance, in form_helper.php you find

PHP Code:
use Config\Services

And then later in the file this simple piece of code uses "services"

PHP Code:
$request Services::request();