반응형
클래스 메서드를 콜백으로 사용하는 방법
콜백으로 사용하는 메서드가 있는 클래스가 있습니다.
어떻게 하면 그들을 논쟁으로 넘길 수 있을까?
Class MyClass {
public function myMethod() {
// How should these be called?
$this->processSomething(this->myCallback);
$this->processSomething(self::myStaticCallback);
}
private function processSomething(callable $callback) {
// Process something...
$callback();
}
private function myCallback() {
// Do something...
}
private static function myStaticCallback() {
// Do something...
}
}
함수를 콜백으로 전달하는 모든 다양한 방법을 보려면 설명서를 확인하십시오.여기 매뉴얼을 복사하고 시나리오에 따라 각 접근법의 예를 추가했습니다.
콜 가능
- PHP 함수는 이름으로 문자열로 전달됩니다.array(), echo, empty(), eval(), exit(), isset(), list(), print 또는 unset()와 같은 언어 구성 요소를 제외한 모든 내장 함수 또는 사용자 정의 함수를 사용할 수 있습니다.
// Not applicable in your scenario
$this->processSomething('some_global_php_function');
- 인스턴스화된 오브젝트의 메서드는 인덱스 0의 오브젝트와 인덱스 1의 메서드명을 포함한 배열로서 전달된다.
// Only from inside the same class
$this->processSomething([$this, 'myCallback']);
$this->processSomething([$this, 'myStaticCallback']);
// From either inside or outside the same class
$myObject->processSomething([new MyClass(), 'myCallback']);
$myObject->processSomething([new MyClass(), 'myStaticCallback']);
- 또한 인덱스 0에 오브젝트 대신 클래스 이름을 전달함으로써 해당 클래스의 오브젝트를 인스턴스화하지 않고 스태틱클래스 메서드를 전달할 수도 있습니다.
// Only from inside the same class
$this->processSomething([__CLASS__, 'myStaticCallback']);
// From either inside or outside the same class
$myObject->processSomething(['\Namespace\MyClass', 'myStaticCallback']);
$myObject->processSomething(['\Namespace\MyClass::myStaticCallback']); // PHP 5.2.3+
$myObject->processSomething([MyClass::class, 'myStaticCallback']); // PHP 5.5.0+
- 일반적인 사용자 정의 함수 외에 익명 함수도 콜백 파라미터에 전달할 수 있습니다.
// Not applicable in your scenario unless you modify the structure
$this->processSomething(function() {
// process something directly here...
});
5.3은 좀 더 우아하게 쓸 수 있는 방법이 있기 때문에 좀 더 줄일 수 있는지 알아보고 있습니다.
$this->processSomething(function() {
$this->myCallback();
});
PHP 8.1을 기점으로 퍼스트 클래스 콜러블이 갖추어져 있습니다.그들은 구문을 사용합니다.$callable = functionName(...)
.
새로운 구문을 사용하여 호출 가능한 클래스 메서드를 만들 수 있습니다.
Class MyClass {
public function myMethod() {
// first-class callables
$this->processSomething($this->myCallback(...));
$this->processSomething(self::myStaticCallback(...));
}
private function processSomething(callable $callback) {
// Process something...
$callback();
}
private function myCallback() {
// Do something...
}
private static function myStaticCallback() {
// Do something...
}
}
call_user_func()를 사용하여 콜백을 지정할 수도 있습니다.
public function myMethod() {
call_user_func(array($this, 'myCallback'));
}
private function myCallback() {
// do something...
}
언급URL : https://stackoverflow.com/questions/28954168/how-to-use-class-methods-as-callbacks
반응형
'programing' 카테고리의 다른 글
나사산이 손실된 경우 테이블 잠금 해제 (0) | 2022.11.03 |
---|---|
JSON을 Ordered Dict에 로드할 수 있습니까? (0) | 2022.11.03 |
MySQL에서 예약된 단어를 테이블 또는 열 이름으로 사용하여 구문 오류가 발생했습니다. (0) | 2022.11.03 |
열을 Base64에서 문자열로 업데이트(SQL 네이티브) (0) | 2022.11.03 |
b-nav-item의 부트스트랩-vue 문제, 색상을 변경할 수 없음 (0) | 2022.11.03 |