
DateTime::__wakeup() 메소드는 PHP 5.2.0부터 사용할 수 있는 메소드입니다. 이 메소드는 클래스의 인스턴스를 리로드할 때 호출되지 않습니다.
이 메소드는 unserialize() 함수를 사용하여 클래스의 인스턴스를 복원할 때 호출됩니다. unserialize() 함수는 serialize() 함수로 serialize된 데이터를 복원하는 함수입니다.
DateTime::__wakeup() 메소드는 클래스의 인스턴스를 복원할 때 호출되어 인스턴스의 속성을 초기화합니다. 이 메소드는 클래스의 인스턴스를 리로드할 때는 호출되지 않습니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
class DateTime {
public $year;
public $month;
public $day;
public function __construct($year, $month, $day) {
$this->year = $year;
$this->month = $month;
$this->day = $day;
}
public function __wakeup() {
echo "DateTime::__wakeup() 메소드가 호출되었습니다.n";
}
}
$date = new DateTime(2022, 12, 25);
$date_str = serialize($date);
unserialize($date_str);
위 코드를 실행하면, "DateTime::__wakeup() 메소드가 호출되었습니다."라는 메시지가 출력됩니다.
따라서, DateTime::__wakeup() 메소드는 unserialize() 함수를 사용하여 클래스의 인스턴스를 복원할 때 호출됩니다.
2025-07-10 15:11