Phalcon events
NOTE
All classes are prefixed with Phalcon
Events\AbstractEventsAware
¶
-
Namespace
Phalcon\Events
-
Uses
Phalcon\Events\ManagerInterface
-
Extends
-
Implements
This abstract class offers access to the events manager
Properties¶
Methods¶
Returns the internal event manager Sets the events managerprotected function fireManagerEvent( string $eventName, mixed $data = null, bool $cancellable = bool ): mixed | bool;
Events\Event
¶
-
Namespace
Phalcon\Events
-
Uses
Phalcon\Contracts\Events\Stoppable
-
Extends
-
Implements
EventInterfaceStoppable
This class offers contextual information of a fired event in the EventsManager
Phalcon\Events\Event;
$event = new Event("db:afterQuery", $this, ["data" => "mydata"], true);
if ($event->isCancelable()) {
$event->stop();
}
Properties¶
/**
* Is event cancelable?
*
* @var bool
*/
protected $cancelable;
/**
* Event data
*
* @var mixed
*/
protected $data;
/**
* Event source
*
* @var object|null
*/
protected $source;
/**
* Is event propagation stopped?
*
* @var bool
*/
protected $stopped = false;
/**
* Event type
*
* @var string
*/
protected $type;
Methods¶
public function __construct( string $type, mixed $source = null, mixed $data = null, bool $cancelable = bool );
stopped flag as isStopped(); calling stop() flips both. Check whether the event is currently stopped. Sets event data. Sets event type. Stops the event preventing propagation. Events\EventInterface
¶
-
Namespace
Phalcon\Events
-
Uses
Phalcon\Contracts\Events\Event
-
Extends
EventContract -
Implements
Phalcon\Events\EventInterface
@psalm-suppress DeprecatedInterface @deprecated Will be removed in a future major release. Use {@see \Phalcon\Contracts\Events\Event} instead.
Events\EventsAwareInterface
¶
-
Namespace
Phalcon\Events
-
Uses
Phalcon\Contracts\Events\EventsAware
-
Extends
EventsAwareContract -
Implements
Phalcon\Events\EventsAwareInterface
@psalm-suppress DeprecatedInterface @deprecated Will be removed in a future major release. Use {@see \Phalcon\Contracts\Events\EventsAware} instead.
Events\Exception¶
-
Namespace
Phalcon\Events
-
Uses
-
Extends
\Exception -
Implements
Exceptions thrown in Phalcon\Events will use this class
Events\Manager¶
-
Namespace
Phalcon\Events
-
Uses
ClosurePhalcon\Contracts\Events\Subscriber
-
Extends
-
Implements
ManagerInterface
Phalcon Events Manager, offers an easy way to intercept and manipulate, if needed, the normal flow of operation. With the EventsManager the developer can create hooks or plugins that will offer monitoring of data, manipulation, conditional execution and much more.
Properties¶
/**
* @var bool
*/
protected $collect = false;
/**
* @var bool
*/
protected $enablePriorities = false;
/**
* Re-entrancy depth of fire()/fireAll(). 0 means no fire is in
* progress. Incremented on every fire entry, decremented on exit.
* Used to keep nested fire() calls from clobbering the outer
* caller's `$this->responses` accumulator.
*
* @var int
*/
protected $fireDepth = ;
/**
* Manager-level kill switch. When true, every fire()/fireAll()/
* fireQueue() call returns immediately (null or empty array) without
* dispatching. Cleared by resume(). Survives across fire() calls,
* unlike Event::stop() which only stops the current dispatch chain.
*
* @var bool
*/
protected $halted = false;
/**
* When true, a listener returning literal `false` (with the event's
* `cancelable` flag on) short-circuits the dispatch loop and pins
* the fire() return as `false`. Default off — preserves the pre-5.13
* "last-wins" contract for codebases that rely on later listeners
* overriding an earlier false return [#17019].
*
* @var bool
*/
protected $stopOnFalse = false;
/**
* When true, fire()/fireAll() throw on dispatch of an event that
* has zero matching listeners. Catches typos in dev. Default off.
*
* @var bool
*/
protected $strict = false;
/**
* Parsed-eventType cache. Memoizes the strpos + substr work done in
* fire() so the same event name fired repeatedly (the common case
* for db:beforeQuery, model:afterSave, etc.) collapses to a single
* hash lookup.
*
* Shape: `eventNameCache[$eventType] = [typePrefix, eventName]`
*
* Unbounded by design — distinct event types in a typical Phalcon
* application are well under 100 keys, and the cache never needs
* invalidation (parse is deterministic for a given eventType string).
*
* @var array
*/
protected $eventNameCache;
/**
* Memoized method_exists() results for the OBJECT_METHOD dispatch
* path in dispatch(). Keyed by `handlerClass => [methodName => bool]`.
* A class doesn't gain methods at runtime so the lookup is permanent.
*
* @var array
*/
protected $methodExistsCache;
/**
* Memoized getSubscribedEvents() maps keyed by Subscriber class name.
* The static method's return is stable for the lifetime of a class
* definition, so the cache never needs invalidation.
*
* @var array
*/
protected $subscriberEventsCache;
/**
* Listener storage. Shape:
*
* events[$eventType] = [
* [handler, type, priority] // types 0, 1, 3
* [handler, type, priority, className] // type 2 carries
* // resolved class name
* ...
* ]
*
* Kept sorted by priority descending when priorities are enabled
* (FIFO within the same priority); otherwise listeners are simply
* appended in attach order.
*
* `type` is classified once at attach() time so dispatch() can
* route via a simple branch:
*
* 0 — Closure: direct invocation via `{handler}(args)`, no
* arg-array alloc per call
* 1 — [obj, method] array callable: direct dynamic dispatch
* `handler[0]->{handler[1]}(args)`
* 2 — plain object: dynamic dispatch via method named after the
* event (the classic Phalcon listener pattern); class name is
* captured at attach time to skip get_class() per fire
* 3 — generic callable (string fn name, invokable object,
* [class, staticMethod]): call_user_func_array
*
* @var array
*/
protected $events;
/**
* @var array
*/
protected $responses;
/**
* @var array
*/
protected $subscribers;
Methods¶
Registers an event subscriber. The subscriber's getSubscribedEvents() map is parsed and each entry is attached through the regular listener pipeline. Returns if priorities are enabledfinal public function attach( string $eventType, mixed $handler, int $priority = static-constant-access ): void;
Iterates a snapshot of subscribers so removeSubscriber() can safely mutate the original property during the walk.
A priority queue of events is a data structure similar to a regular queue of events: we can also put and extract elements from it. The difference is that each element in a priority queue is associated with a value called priority. This value is used to order elements of a queue: elements with higher priority are retrieved before the elements with lower priority.
final public function fire( string $eventType, object $source, mixed $data = null, bool $cancelable = bool );
public function fireAll( string $eventType, object $source, mixed $data = null, bool $cancelable = bool ): array;
$this->responses is preserved (stashed and restored across the call). Internal handler to call a queue of events. Kept at its original 2-arg signature for BC; thin wrapper around the private dispatch() helper. Direct callers pay the cost of re-extracting metadata from the Event; the framework's own fire() path bypasses this wrapper and calls dispatch() with hoisted args.
false (with cancelable=true) stops the current event's queue and pins the fire() return as false. Later listeners cannot overwrite the cancel. Default off. Independent of halt() / event->stop() — only governs how the dispatch loop reacts to a false listener return.
Events\ManagerInterface
¶
-
Namespace
Phalcon\Events
-
Uses
Phalcon\Contracts\Events\Manager
-
Extends
ManagerContract -
Implements
Phalcon\Events\ManagerInterface
@psalm-suppress DeprecatedInterface @deprecated Will be removed in a future major release. Use {@see \Phalcon\Contracts\Events\Manager} instead.