mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #5790 from kenjis/fix-docs-events
docs: fix events.rst
This commit is contained in:
commit
d95e46101d
@ -4,9 +4,9 @@ Replacing Common Functions
|
|||||||
|
|
||||||
There are quite a few functions necessary to CodeIgniter that need to be loaded early for use in the core classes and
|
There are quite a few functions necessary to CodeIgniter that need to be loaded early for use in the core classes and
|
||||||
thus cannot be placed into a helper. While most users will never have any need to do this, but the option to replace
|
thus cannot be placed into a helper. While most users will never have any need to do this, but the option to replace
|
||||||
these functions does exist for those who would like to significantly alter the CodeIgniter core. In the ``App\``
|
these functions does exist for those who would like to significantly alter the CodeIgniter core. In the **app/**
|
||||||
directory there is a file ``Common.php``, and any functions defined in there will take precedence over the versions
|
directory there is a file **Common.php**, and any functions defined in there will take precedence over the versions
|
||||||
found in ``system/Common.php``. This is also an opportunity to create globally-available functions you intend to
|
found in **system/Common.php**. This is also an opportunity to create globally-available functions you intend to
|
||||||
use throughout the framework.
|
use throughout the framework.
|
||||||
|
|
||||||
.. note:: Messing with a core system class has a lot of implications, so make sure you know what you are doing before
|
.. note:: Messing with a core system class has a lot of implications, so make sure you know what you are doing before
|
||||||
|
@ -24,13 +24,13 @@ Defining an Event
|
|||||||
=================
|
=================
|
||||||
|
|
||||||
Most events are defined within the **app/Config/Events.php** file. You can subscribe an action to an event with
|
Most events are defined within the **app/Config/Events.php** file. You can subscribe an action to an event with
|
||||||
the Events class' ``on()`` method. The first parameter is the name of the event to subscribe to. The second parameter is
|
the ``Events`` class' ``on()`` method. The first parameter is the name of the event to subscribe to. The second parameter is
|
||||||
a callable that will be run when that event is triggered:
|
a callable that will be run when that event is triggered:
|
||||||
|
|
||||||
.. literalinclude:: events/001.php
|
.. literalinclude:: events/001.php
|
||||||
|
|
||||||
In this example, whenever the **pre_controller** event is executed, an instance of ``MyClass`` is created and the
|
In this example, whenever the ``pre_system`` event is executed, an instance of ``MyClass`` is created and the
|
||||||
``MyFunction`` method is run. Note that the second parameter can be *any* form of
|
``myFunction()`` method is run. Note that the second parameter can be *any* form of
|
||||||
`callable <https://www.php.net/manual/en/function.is-callable.php>`_ that PHP recognizes:
|
`callable <https://www.php.net/manual/en/function.is-callable.php>`_ that PHP recognizes:
|
||||||
|
|
||||||
.. literalinclude:: events/002.php
|
.. literalinclude:: events/002.php
|
||||||
|
@ -2,4 +2,4 @@
|
|||||||
|
|
||||||
use CodeIgniter\Events\Events;
|
use CodeIgniter\Events\Events;
|
||||||
|
|
||||||
Events::on('pre_system', ['MyClass', 'MyFunction']);
|
Events::on('pre_system', ['MyClass', 'myFunction']);
|
||||||
|
@ -5,7 +5,7 @@ Events::on('pre_system', 'some_function');
|
|||||||
|
|
||||||
// Call on an instance method
|
// Call on an instance method
|
||||||
$user = new User();
|
$user = new User();
|
||||||
Events::on('pre_system', [$user, 'some_method']);
|
Events::on('pre_system', [$user, 'someMethod']);
|
||||||
|
|
||||||
// Call on a static method
|
// Call on a static method
|
||||||
Events::on('pre_system', 'SomeClass::someMethod');
|
Events::on('pre_system', 'SomeClass::someMethod');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user