xp_emit

xp_emit($signal[, $context = false])

Emits a signal.

This will execute all processes, before and after functions installed to the signal.

Returns the executed \XPSPL\SIG object.

Note

When emitting unique signals, e.g.. complex, routines or defined uniques the unique \XPSPL\SIG object installed must be provided.

Once a signal is emitted the following execution chain takes place.

  1. Before process functions
  2. Process function
  3. After process function
Parameters:
  • signal – XPSPLSIG object to emit.
  • object – XPSPLSIG object context to execute within.
Return type:

object XPSPLSIG

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 Unique Signals

<?php

// Create a unique Foo signal.
class SIG_Foo extends \XPSPL\SIG {
    // declare it as unique
    protected $_unique = true;
}

// Create a SIG_Foo unique object
$foo = new SIG_Foo();

signal($foo, function(){
    echo "foo";
});

// Emit the SIG_Foo and another unique SIG_Foo
xp_emit($foo);
xp_emit(new Foo());

The above code will output.

foo

Example #3 Complex Signals

<?php

// Create our 3 required signals.
class SIG_Bells extends \XPSPL\SIG {}
class SIG_Bride extends \XPSPL\SIG {}
class SIG_Groom extends \XPSPL\SIG {}

// Create a complex signal that will emit when a wedding takes place
// based on 3 seperate signals, SIG_Bells, SIG_Bride and SIG_Groom.
class SIG_Wedding extends \XPSPL\SIG_Complex {

    // Keep track if each requirement has already emitted
    protected $_bells = false;
    protected $_bride = false;
    protected $_groom = false;

    // Create an evaulation method to evaluate the runtime
    public function evaluate($signal = null)
    {
      switch ($signal) {
        case $signal instanceof SIG_Bells:
            $this->_bells = true;
            break;
        case $signal instanceof SIG_Bride:
            $this->_bride = true;
            break;
        case $signal instanceof SIG_Groom:
            $this->_groom = true;
            break;
      }
      if ($this->_bells === true &&
            $this->_bride === true &&
            $this->_groom === true) {
        $this->_bells = false;
        $this->_groom = false;
        $this->_bride = false;
        return true;
      }
      return false;
    }
}

// Install a process for complex signal.
xp_signal(new SIG_Wedding(), function(){
    echo 'The wedding is happening!'.PHP_EOL;
});

// Emit SIG_Bells, SIG_Bride and SIG_Groom
xp_emit(new SIG_Bells());
xp_emit(new SIG_Bride());
xp_emit(new SIG_Groom());

The above code will output.

The wedding is happening!
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.