채택완료

Hook이 안 되는 이유가 뭘까요

1. PHP 5.3 이상

Copy
add_event('tag', function () {
   echo 'Ok';
});
run_event('tag');

2. PHP 7~8

Copy
class SomeClass()
{
    protected string $name;

    function __construct(string $name)
    {
        $this->name = $name;
    }

    public static someMethod()
    {
        echo 'Ok';
    }
}

add_event('tag', 'SomeClass::someMethod');
add_event('tag', ['SomeClass', 'someMethod']);
run_event('tag');

3. PHP 7~8

Copy
class SomeClass()
{
    protected string $name;

    function __construct(string $name)
    {
         $this->name = $name;
    }

    public someMethod()
    {
        echo 'Ok';
    }
}

$instance = new SomeClass();
add_event('tag', [$instance, 'someMethod']);
run_event('tag');

|

답변 1개 / 댓글 1개

채택된 답변
+20 포인트

1. PHP Warning:  function_exists() expects parameter 1 to be string

anonymous function 은 현재 v.5.5.8.3.1 버전 구조에서 동작하지 않습니다.

https://github.com/gnuboard/gnuboard5/blob/v5.5.8.3.1/lib/Hook/hook.extends.class.php#L22

function_exists 는 문자열만 인자로 받기 때문인데 is_callable 정도로 변경시 동작하며

이와 관련해 수정코드가 최근 merge 된것 같습니다.

https://github.com/gnuboard/gnuboard5/commit/81792d25957f68126d53d45fca8bc15de088123a

위 사항이 반영되지 않은 기존 function_exists 환경에서는 다음처럼 처리할수 있습니다.

Copy
function fn_tag() {
    echo 'Ok';
}
add_event('tag', 'fn_tag');
run_event('tag');

 

2. 클래스 문법 에러 및 정적 호출시 라이브러리 단에서 function_exists 가 아닌 is_callable 필요

Copy
//class SomeClass() // Parse error: syntax error, unexpected '(', expecting '{'
class SomeClass
{
    protected string $name;
    //function __construct(string $name)

    public function __construct(string $name)
    {   
        $this->name = $name;
    }   
    //public static someMethod() // PHP Parse error:  syntax error, unexpected '(', expecting variable (T_VARIABLE)
    public static function someMethod()
    {   
        echo 'Ok';
    }   
}
add_event('tag', 'SomeClass::someMethod'); // function_exists > is_callable
//add_event('tag', ['SomeClass', 'someMethod']);
run_event('tag');

 

3. 정적 형태 호출이 아닌 인스턴스 형태의 호출에 대한 생성자 수정 및 추가처리 필요

Copy
//class SomeClass() // Parse error: syntax error, unexpected '(', expecting '{'
class SomeClass
{
    private static $instance;
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new static();
        }
        return self::$instance;
    }   

    protected string $name;
    // Fatal error: Uncaught ArgumentCountError: Too few arguments to function SomeClass::__construct(), 0 passed
    //function __construct(string $name)
    //{ 
    //    $this->name = $name;
    //} 
    //public static someMethod() // PHP Parse error:  syntax error, unexpected '(', expecting variable (T_VARIABLE)
    public function someMethod()
    {   
        echo 'Ok';
    }   

    public function __construct() {}
    public function setName($name) { $this->name = $name; }
    public function getName() { return $this->name; }
}

 

/**

 * method must be static : public static function someMethod()

 *     (Deprecated: Non-static method SomeClass::someMethod() should not be called statically)

 * function_exists > is_callable

 */

// add_event('tag', 'SomeClass::someMethod');


add_event('tag', ['SomeClass', 'someMethod']);
//run_event('tag');

$instance = new SomeClass();
add_event('tag', [$instance, 'someMethod']);
run_event('tag');

답변에 대한 댓글 1개

역시...
항상 월등한 좋은 답변 잘 보고있습니다.

답변을 작성하려면 로그인이 필요합니다.