PHP code example of yzh52521 / webman-event

1. Go to this page and download the library: Download yzh52521/webman-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yzh52521 / webman-event example snippets


return [
    'enable'      => true,
    'events'       =>[
            // 事件监听
            'listener'    => [
                'test' => [
                    \app\listeners\TestListeners::class,
                ],
            ],
        
            // 事件订阅器
            'subscriber' => [
               \app\subscribes\TestSubscribe::class,
            ],
    ]
];

namespace app\events;

class Test
{
    public  $data = [];

    public function __construct($data)
    {
        $this->data = $data;
    }
}

namespace app\listeners;

use app\events\Test;

class TestListeners
{
    public function __construct()
    {
    }

    /**
     * 处理事件
     * @return void
     */
    public function handle(Test $event)
    {
        // 控制台打印
        var_dump('listener');
        var_dump($event->data);
    }
}

namespace app\subscribes;

use app\events\Test;

class TestSubscribe
{
    public function handleTest(Test $event)
    {
        var_dump('subscribe');
        var_dump($event);
    }

    public function subscribe($events)
    {
        $events->listen(
            Test::class,
            [TestSubscribe::class, 'handleTest']
        );
    }
}