class TestObj { // 이벤트 핸들러 등록 public function register() { add_event('test_event', [$this, 'test']); } // 이벤트 핸들러 메소드 public function test() { die('test event fired'); } } // 객체 생성 및 이벤트 핸들러 등록 $obj = new TestObj(); $obj->register(); // 이벤트 퐈이어 run_event('test_event');
==========
위의 코드처럼 사용자 클래스를 정의하고, 이벤트 핸들러를 등록하여 사용하면 될줄 알았는데 안되네요. Hook 라이브러리 git repo 에 있는 메뉴얼을 봐서는 될 것 같은데.. 좀 당황 ^^;;
다음과 같이 사용하실 수 있습니다.
class TestObj { // 이벤트 핸들러 메소드 public function test() { die('test event fired'); } } // 이벤트 등록 add_event('test_event', ['TestObj', 'test']); // 이벤트 퐈이어 run_event('test_event');
==========
또는.. 인스턴스를 생성하고 추가적인 작업이 필요한 경우에는 싱글톤을 사용하시면 됩니다.
class TestObj { // 싱글톤 인스턴스 private static $instance = null; // 인스턴스 접근 메소드 public static function getInstance() { if(!self::$instance) { self::$instance = new TestObj(); } return self::$instance; } // 프라이빗 생성자 (중복 생성 방지) private function __construct() { } // 이벤트 핸들러 public function test() { die('test event fired'); } } $instance = TestObj::getInstance(); // .. do something with $instance add_event('test_event', [$instance, 'test']); run_event('test_event');
댓글 2개
게시글 목록
| 번호 | 제목 |
|---|---|
| 194 | |
| 184 | |
| 169 | |
| 163 | |
| 160 | |
| 143 | |
| 132 | |
| 130 | |
| 129 | |
| 127 | |
| 125 | |
| 113 | |
| 109 | |
| 101 | |
| 94 | |
| 93 | |
| 86 | |
| 78 | |
| 74 | |
| 71 | |
| 51 | |
| 43 | |
| 42 | |
| 33 | |
| 14 | |
| 12 | |
| 4 | |
| 2 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기