Hook이 안 되는 이유가 뭘까요

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
Hook이 안 되는 이유가 뭘까요

QA

Hook이 안 되는 이유가 뭘까요

본문

1. PHP 5.3 이상


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

2. PHP 7~8


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


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. 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 환경에서는 다음처럼 처리할수 있습니다.


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

 

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


//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. 정적 형태 호출이 아닌 인스턴스 형태의 호출에 대한 생성자 수정 및 추가처리 필요


//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');
답변을 작성하시기 전에 로그인 해주세요.
전체 3

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT