W3cubDocs

/CakePHP 4.4

Class Event

Class Event

Namespace: Cake\Event

Property Summary

  • $_data protected
    array

    Custom data for the method that receives the event

  • $_name protected
    string

    Name of the event

  • $_stopped protected
    bool

    Flags an event as stopped or not, default is false

  • $_subject protected
    object|null

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

  • $result protected
    mixed

    Property used to retain the result value of the event listeners

Method Summary

  • __construct() public

    Constructor

  • getData() public

    Access the event data/payload.

  • getName() public

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

  • getResult() public

    The result value of the event listeners

  • getSubject() public

    Returns the subject of this event

  • isStopped() public

    Check if the event is stopped

  • setData() public

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

  • setResult() public

    Listeners can attach a result value to the event.

  • stopPropagation() public

    Stops the event from being used anymore

Method Detail

__construct() public

__construct(string $name, object|null $subject = null, ArrayAccess|array|null $data = null)

Constructor

Examples of usage:

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

Parameters

string $name

Name of the event

object|null $subject optional

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

ArrayAccess|array|null $data optional

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

getData() public

getData(string|null $key = null): mixed|array|null

Access the event data/payload.

Parameters

string|null $key optional

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

Returns

mixed|array|null

getName() public

getName(): string

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

Returns

string

getResult() public

getResult(): mixed

The result value of the event listeners

Returns

mixed

getSubject() public

getSubject(): object

Returns the subject of this event

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

Returns

object

Throws

Cake\Core\Exception\CakeException

isStopped() public

isStopped(): bool

Check if the event is stopped

Returns

bool

setData() public

setData(array|string $key, mixed $value = null): $this

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

Parameters

array|string $key

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

mixed $value optional

The value to set.

Returns

$this

setResult() public

setResult(mixed $value = null): $this

Listeners can attach a result value to the event.

Parameters

mixed $value optional

The value to set.

Returns

$this

stopPropagation() public

stopPropagation(): void

Stops the event from being used anymore

Returns

void

Property Detail

$_data protected

Custom data for the method that receives the event

Type

array

$_name protected

Name of the event

Type

string

$_stopped protected

Flags an event as stopped or not, default is false

Type

bool

$_subject protected

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

Type

object|null

$result protected

Property used to retain the result value of the event listeners

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

Type

mixed

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Event.Event.html