You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/concepts/services.rst
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ come in handy.
37
37
Instead of creating the instance ourself, we let a central class create an instance of the
38
38
class for us. This class is kept very simple. It only contains a method for each class that we want
39
39
to use as a service. The method typically returns a **shared instance** of that class, passing any dependencies
40
-
it might have into it. Then, we would replace our timer creation code with code that calls this new class:
40
+
it might have into it. Then, we would replace our timer creation code with code that calls this global function or Services class:
41
41
42
42
.. literalinclude:: services/002.php
43
43
@@ -55,7 +55,7 @@ As many CodeIgniter classes are provided as services, you can get them like the
55
55
56
56
.. literalinclude:: services/013.php
57
57
58
-
The ``$typography`` is an instance of the Typography class, and if you call ``\Config\Services::typography()`` again, you will get the exactly same instance.
58
+
The ``$timer`` is an instance of the Timer class, and if you call ``service('timer')`` again, you will get the exactly same instance.
59
59
60
60
The Services typically return a **shared instance** of the class. The following code creates a ``CURLRequest`` instance at the first call. And the second call returns the exactly same instance.
61
61
@@ -66,7 +66,7 @@ Therefore, the parameter ``$options2`` for the ``$client2`` does not work. It is
66
66
Getting a New Instance
67
67
======================
68
68
69
-
If you want to get a new instance of the Typography class, you need to pass ``false`` to the argument ``$getShared``:
69
+
If you want to get a new instance of the Timer class, you need to pass ``false`` to the argument ``$getShared``:
70
70
71
71
.. literalinclude:: services/014.php
72
72
@@ -85,6 +85,8 @@ always return the same instance:
85
85
86
86
.. literalinclude:: services/003.php
87
87
88
+
.. note:: Since v4.5.0, when you don't pass parameters to the service, the global function ``service()`` is recommended due to performance improvements.
89
+
88
90
If the creation method requires additional parameters, they can be passed after the service name:
0 commit comments