Fr3nch13/CakePHP Utilities

Event
in package
implements EventInterface

Class Event

Tags
template

TSubject

implements

\Cake\Event\EventInterface<TSubject>

Table of Contents

Interfaces

EventInterface
Represents the transport class of events across the system. It receives a name, subject and an optional payload. The name can be any string that uniquely identifies the event across the application, while the subject represents the object that the event applies to.

Properties

$_data  : array<string|int, mixed>
Custom data for the method that receives the event
$_name  : string
Name of the event
$_stopped  : bool
Flags an event as stopped or not, default is false
$_subject  : object|null
The object this event applies to (usually the same object that generates the event)
$result  : mixed
Property used to retain the result value of the event listeners

Methods

__construct()  : mixed
Constructor
getData()  : mixed|array<string|int, mixed>|null
Access the event data/payload.
getName()  : string
Returns the name of this event. This is usually used as the event identifier
getResult()  : mixed
The result value of the event listeners
getSubject()  : object
Returns the subject of this event
isStopped()  : bool
Check if the event is stopped
setData()  : $this
Assigns a value to the data/payload of this event.
setResult()  : $this
Listeners can attach a result value to the event.
stopPropagation()  : void
Stops the event from being used anymore

Properties

$_data

Custom data for the method that receives the event

protected array<string|int, mixed> $_data

$_name

Name of the event

protected string $_name

$_stopped

Flags an event as stopped or not, default is false

protected bool $_stopped = false

$_subject

The object this event applies to (usually the same object that generates the event)

protected object|null $_subject
Tags
psalm-var

TSubject|null

$result

Property used to retain the result value of the event listeners

protected mixed $result

Use setResult() and getResult() to set and get the result.

Methods

__construct()

Constructor

public __construct(string $name[, object|null $subject = null ][, ArrayAccess|array<string|int, mixed>|null $data = null ]) : mixed

Examples of usage:

 $event = new Event('Order.afterBuy', $this, ['buyer' => $userData]);
 $event = new Event('User.afterRegister', $userModel);
Parameters
$name : string

Name of the event

$subject : object|null = null

the object that this event applies to (usually the object that is generating the event).

$data : ArrayAccess|array<string|int, mixed>|null = null

any value you wish to be transported with this event to it can be read by listeners.

Tags
psalm-param

TSubject|null $subject

getData()

Access the event data/payload.

public getData([string|null $key = null ]) : mixed|array<string|int, mixed>|null
Parameters
$key : string|null = null

The data payload element to return, or null to return all data.

Return values
mixed|array<string|int, mixed>|null

The data payload if $key is null, or the data value for the given $key. If the $key does not exist a null value is returned.

getName()

Returns the name of this event. This is usually used as the event identifier

public getName() : string
Return values
string

getResult()

The result value of the event listeners

public getResult() : mixed

getSubject()

Returns the subject of this event

public getSubject() : object

If the event has no subject an exception will be raised.

Tags
throws
CakeException
psalm-return

TSubject

Return values
object

isStopped()

Check if the event is stopped

public isStopped() : bool
Return values
bool

True if the event is stopped

setData()

Assigns a value to the data/payload of this event.

public setData(array<string|int, mixed>|string $key[, mixed $value = null ]) : $this
Parameters
$key : array<string|int, mixed>|string

An array will replace all payload data, and a key will set just that array item.

$value : mixed = null

The value to set.

Return values
$this

setResult()

Listeners can attach a result value to the event.

public setResult([mixed $value = null ]) : $this
Parameters
$value : mixed = null

The value to set.

Return values
$this

stopPropagation()

Stops the event from being used anymore

public stopPropagation() : void

        
On this page

Search results