xp_signal

xp_signal($signal, $process)

Registers a process to execute when the given signal is emitted.

Note

All processes by default have an exhaust of null.

Note

Processes installed to the same signal execute in FIFO order when no priority is defined.

Parameters:
  • object – Signal to install process on.
  • object – PHP Callable
Return type:

object | boolean - XPSPLProcess otherwise boolean on error

Note

Beginning in XPSPL v4.0.0 all signals were converted to strictly objects.

To use a string or integer as a signal it must be wrapped in a XP_SIG.

Warning

Any signal created using `XP_SIG` CANNOT be unique.

Example #1 Basic Usage

<?php

// Install a process on the foo signal
xp_signal(XP_SIG('foo'), function(){
    echo 'foo';
});

// Emit the foo signal
xp_emit(XP_SIG('foo'));

The above example will output.

foo

Example #2 Class Signals

<?php

// Declare a simple signal
class SIG_Basic extends \XPSPL\SIG {}

// Install a process on the SIG_Basic class
xp_signal(new SIG_Basic(), function(){
    echo 'foo';
})

xp_emit(new SIG_Basic());

The above example will output.

foo

Example #3 Exhausting Processes

<?php

// Install a process on the foo signal, with an exhaust of 1
xp_signal(XP_SIG('foo', xp_exhaust(1, function(){
    echo 'foo';
})));

// Emit the foo signal
xp_emit(XP_SIG('foo'));
xp_emit(XP_SIG('foo'));

The above code will output.

foo

Example #4 Unique Signals

<?php

// Declare a simple unique
class SIG_Foo extends \XPSPL\SIG {
    // Set the signal as unique
    protected $_unqiue = true;
}

// Create two unique SIG_Foo objects
$sig_foo_1 = new SIG_Foo();
$sig_foo_2 = new SIG_Foo();

// Install a process to each unique signal
xp_signal($sig_foo_1, function(){
    echo 'foo';
});

xp_signal($sig_foo_2, function(){
    echo 'bar';
})

// Emit each unique signal
xp_emit($sig_foo_1);
xp_emit($sig_foo_2);

The above code will output.

foobar
Read the Docs v: v5.0.1
Versions
latest
v5.0.1
v5.0.0
v4.0.1
v4.0.0
v3.1.1
v3.1.0
v3.0.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.