Hook이 안 되는 이유가 뭘까요 채택완료
1. PHP 5.3 이상
add_event('tag', function () {
echo 'Ok';
});
run_event('tag');
2. PHP 7~8
class SomeClass()
{
protected string $name;</p>
<p> function __construct(string $name)
{
$this->name = $name;
}</p>
<p> public static someMethod()
{
echo 'Ok';
}
}</p>
<p>add_event('tag', 'SomeClass::someMethod');
add_event('tag', ['SomeClass', 'someMethod']);
run_event('tag');
3. PHP 7~8
class SomeClass()
{
protected string $name;</p>
<p> function __construct(string $name)
{
$this->name = $name;
}</p>
<p> public someMethod()
{
echo 'Ok';
}
}</p>
<p>$instance = new SomeClass();
add_event('tag', [$instance, 'someMethod']);
run_event('tag');
댓글을 작성하려면 로그인이 필요합니다.
답변 1개
채택된 답변
+20 포인트
2년 전
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 환경에서는 다음처럼 처리할수 있습니다.
</p>
<p>function fn_tag() {
echo 'Ok';
}
add_event('tag', 'fn_tag');
run_event('tag');</p>
<p>
2. 클래스 문법 에러 및 정적 호출시 라이브러리 단에서 function_exists 가 아닌 is_callable 필요
</p>
<p>//class SomeClass() // Parse error: syntax error, unexpected '(', expecting '{'
class SomeClass
{
protected string $name;
//function __construct(string $name)</p>
<p> 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');</p>
<p>
3. 정적 형태 호출이 아닌 인스턴스 형태의 호출에 대한 생성자 수정 및 추가처리 필요
</p>
<p>//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;
} </p>
<p> 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';
} </p>
<p> public function __construct() {}
public function setName($name) { $this->name = $name; }
public function getName() { return $this->name; }
}</p>
<p> </p>
<p>/**</p>
<p> * method must be static : public static function someMethod()</p>
<p> * (Deprecated: Non-static method SomeClass::someMethod() should not be called statically)</p>
<p> * function_exists > is_callable</p>
<p> */</p>
<p>// add_event('tag', 'SomeClass::someMethod');</p>
<p>
add_event('tag', ['SomeClass', 'someMethod']);
//run_event('tag');</p>
<p>$instance = new SomeClass();
add_event('tag', [$instance, 'someMethod']);
run_event('tag');</p>
<p>
로그인 후 평가할 수 있습니다
답변에 대한 댓글 1개
j
jihan?
2년 전
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인전체 질문 목록
채택
채택
답변대기
답변대기
채택
채택
채택
채택
채택
채택
채택
채택
답변대기
채택
답변대기
답변대기
채택
채택
답변대기
채택
항상 월등한 좋은 답변 잘 보고있습니다.