Registering events to listen and alter your objects
Introduction
Porte give you the opportunity to register PHP callbacks to be called at specific moments of your records lifecycle.
Such events include:
- before/after a record is created
- before/after a record is updated
- before/after a record is saved
- before/after a record is deleted
An event is defined as the combinaison of an action key and a valid PHP callback.
The same arguments can be used to connect new events as well as to diconnect registered events. An same event is unique per object, which means that even if an event is registered twice, it will only be executed once.
Events scope
There exist 3 types of events given their applyable scope
- Instance level events are specific to a given record.
- Static records level events are specific to a given type of record (sharing a same database table)
- Static global level events are shared by all your records whatever their type.
Instance events versus static events
All the methods related to instance level events are PHP object methods and are accessed through the "events" keyword (returning a "PorteRecordEvents" object).
All the method related to static events are PHP static methods and are accessible through the PorteEvents class The arguments in the static methods are the same as the one of their respective instance method with the addition of the first argument being the table name in the case of static records level events.
Expected behavior while calling events
If any event called in the event chain returns a value different than null, the chain call process is stopped and the value is returned to the caller.
All arguments are passed as reference, bypassing the reference declaration in the callback arguments.