
SwooleHttpResponse::__destruct 메서드는 HTTP 응답 객체를 종료할 때 호출되는 메서드입니다. 이 메서드는 객체가 소멸될 때 호출되며, HTTP 응답을 전송하기 전에 호출됩니다.
이 메서드는 HTTP 헤더를 전송하기 전에 호출되므로, HTTP 헤더를 전송하기 전에 필요한 처리를 수행해야 합니다. 예를 들어, HTTP 상태 코드를 설정하거나, HTTP 헤더를 추가할 수 있습니다.
일반적인 예제는 다음과 같습니다.
#hostingforum.kr
php
$response = new SwooleHttpResponse();
$response->status(200);
$response->header('Content-Type', 'text/plain');
$response->end('Hello, World!');
이 예제에서, `__destruct` 메서드는 자동으로 호출되지 않습니다. 하지만, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$response = new SwooleHttpResponse();
$response->status(200);
$response->header('Content-Type', 'text/plain');
$response->end('Hello, World!');
unset($response);
이 경우, `$response` 객체가 소멸될 때 `__destruct` 메서드가 호출됩니다.
이 메서드를 사용하는 예제는 다음과 같습니다.
#hostingforum.kr
php
class MyResponse extends SwooleHttpResponse {
public function __destruct() {
$this->header('X-My-Header', 'Hello, World!');
}
}
$response = new MyResponse();
$response->status(200);
$response->end('Hello, World!');
이 예제에서, `MyResponse` 클래스는 `Swoole\Http\Response` 클래스를 상속하고, `__destruct` 메서드를 오버라이드합니다. 이 메서드는 HTTP 헤더를 추가합니다.
이 메서드를 사용하는 경우, HTTP 헤더를 전송하기 전에 필요한 처리를 수행할 수 있습니다.
2025-05-06 23:09