라이브러리

[PHP] EventUtil::__construct - 추상 생성자




EventUtil::__construct


EventUtil은 PHP의 EventDispatcher를 사용하여 이벤트를 처리하는 클래스입니다. EventDispatcher는 PHP 8.0 이상에서 사용할 수 있습니다. EventUtil은 EventDispatcher의 기능을 확장하여 이벤트를 처리하는 데 도움이 됩니다.

EventUtil::__construct는 클래스의 생성자 함수로, EventDispatcher를 초기화하는 역할을 합니다.

# 예제


#hostingforum.kr
php

// EventUtil.php

class EventUtil extends EventDispatcher

{

    public function __construct()

    {

        parent::__construct();

    }



    public function onEvent($eventName, $callback)

    {

        $this->on($eventName, $callback);

    }

}



#hostingforum.kr
php

// main.php

require_once 'EventUtil.php';



$eventUtil = new EventUtil();



// 이벤트 등록

$eventUtil->on('hello', function () {

    echo "Hello, World!
";

});



// 이벤트 발생

$eventUtil->dispatch('hello');



# 설명


* `EventUtil` 클래스는 `EventDispatcher`를 상속받아 이벤트를 처리합니다.
* `__construct` 메서드는 `EventDispatcher`를 초기화합니다.
* `onEvent` 메서드는 이벤트를 등록하는 메서드로, `$eventName`과 `$callback`을 인자로 받습니다.
* `$eventUtil` 객체를 생성하고, `hello` 이벤트를 등록합니다.
* `hello` 이벤트를 발생시켜, 등록된 콜백 함수가 호출됩니다.

# 사용 예시


EventUtil을 사용하여 이벤트를 처리하는 예시는 다음과 같습니다.

#hostingforum.kr
php

// EventUtil.php

class EventUtil extends EventDispatcher

{

    public function __construct()

    {

        parent::__construct();

    }



    public function onEvent($eventName, $callback)

    {

        $this->on($eventName, $callback);

    }



    public function onEventWithArg($eventName, $callback)

    {

        $this->on($eventName, function ($arg) use ($callback) {

            return $callback($arg);

        });

    }

}



#hostingforum.kr
php

// main.php

require_once 'EventUtil.php';



$eventUtil = new EventUtil();



// 이벤트 등록

$eventUtil->onEvent('hello', function () {

    echo "Hello, World!
";

});



// 이벤트 등록 (인자 전달)

$eventUtil->onEventWithArg('helloWithArg', function ($arg) {

    echo "Hello, World! ($arg)
";

});



// 이벤트 발생

$eventUtil->dispatch('hello');

$eventUtil->dispatch('helloWithArg', 'PHP');



# 이벤트 처리


EventUtil을 사용하여 이벤트를 처리하는 방법은 다음과 같습니다.

* `on` 메서드를 사용하여 이벤트를 등록합니다.
* `dispatch` 메서드를 사용하여 이벤트를 발생시킵니다.
* 콜백 함수를 등록하여 이벤트를 처리합니다.

# 이벤트 인자 전달


EventUtil을 사용하여 이벤트 인자를 전달하는 방법은 다음과 같습니다.

* `onEventWithArg` 메서드를 사용하여 이벤트를 등록합니다.
* 콜백 함수를 등록하여 이벤트 인자를 처리합니다.

# 이벤트 처리 예시


EventUtil을 사용하여 이벤트를 처리하는 예시는 다음과 같습니다.

#hostingforum.kr
php

// EventUtil.php

class EventUtil extends EventDispatcher

{

    public function __construct()

    {

        parent::__construct();

    }



    public function onEvent($eventName, $callback)

    {

        $this->on($eventName, $callback);

    }



    public function onEventWithArg($eventName, $callback)

    {

        $this->on($eventName, function ($arg) use ($callback) {

            return $callback($arg);

        });

    }

}



#hostingforum.kr
php

// main.php

require_once 'EventUtil.php';



$eventUtil = new EventUtil();



// 이벤트 등록

$eventUtil->onEvent('hello', function () {

    echo "Hello, World!
";

});



// 이벤트 등록 (인자 전달)

$eventUtil->onEventWithArg('helloWithArg', function ($arg) {

    echo "Hello, World! ($arg)
";

});



// 이벤트 발생

$eventUtil->dispatch('hello');

$eventUtil->dispatch('helloWithArg', 'PHP');



# 이벤트 처리 결과


EventUtil을 사용하여 이벤트를 처리하는 결과는 다음과 같습니다.

* `hello` 이벤트가 발생하면 "Hello, World!"가 출력됩니다.
* `helloWithArg` 이벤트가 발생하면 "Hello, World! (PHP)"가 출력됩니다.

# 결론


EventUtil을 사용하여 이벤트를 처리하는 방법은 다음과 같습니다.

* `EventUtil` 클래스를 사용하여 이벤트를 처리합니다.
* `on` 메서드를 사용하여 이벤트를 등록합니다.
* `dispatch` 메서드를 사용하여 이벤트를 발생시킵니다.
* 콜백 함수를 등록하여 이벤트를 처리합니다.

EventUtil을 사용하여 이벤트를 처리하는 예시는 다음과 같습니다.

#hostingforum.kr
php

// EventUtil.php

class EventUtil extends EventDispatcher

{

    public function __construct()

    {

        parent::__construct();

    }



    public function onEvent($eventName, $callback)

    {

        $this->on($eventName, $callback);

    }



    public function onEventWithArg($eventName, $callback)

    {

        $this->on($eventName, function ($arg) use ($callback) {

            return $callback($arg);

        });

    }

}



#hostingforum.kr
php

// main.php

require_once 'EventUtil.php';



$eventUtil = new EventUtil();



// 이벤트 등록

$eventUtil->onEvent('hello', function () {

    echo "Hello, World!
";

});



// 이벤트 등록 (인자 전달)

$eventUtil->onEventWithArg('helloWithArg', function ($arg) {

    echo "Hello, World! ($arg)
";

});



// 이벤트 발생

$eventUtil->dispatch('hello');

$eventUtil->dispatch('helloWithArg', 'PHP');



# 참고 자료


* [PHP 8.0 EventDispatcher](https://www.php.net/manual/en/class.eventdispatcher.php)
* [EventUtil](https://github.com/your-project/event-util)
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 573 페이지

검색

게시물 검색