We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Is there a way to filter/transform a simple variable using Events?


Is there a way to use events to filter variables? I'd like to hook some filters (with different levels of priority) and the event system should work. The only way I've figured out was creating a wrapper class to the value:

class EventFilter {

    private $value;

    public function __construct($value)

    public function setValue($value)
        $this->value = $value;

    public function getValue()
        return $this->value;


And use it:

$messages = new EventFilter($messages);
$di->getShared('eventsManager')->fire('translate:messages', $messages);
$messages = $messages->getValue();

$di->getShared('eventsManager')->attach('translate:messages', function($event, EventFilter $ef){
    $messages = $ef->getValue();

    // Transform the $messages


Is there a better way?

Your code is fine. Priorities can be set by the third argument to attach():

$eventsManager->attach('translate:messages', function() {}, 150); // make 150 less to decrease the priority