라이브러리
[PHP] WeakReference::__construct - 인스턴스화를 허용하지 않는 생성자
WeakReference::__construct
PHP 7.4 버전부터 WeakReference 클래스가 추가되었습니다. 이 클래스는 PHP에서 사용할 수 있는 WeakReference를 제공합니다. WeakReference는 객체를 참조하는 대신, 객체의 주소만 저장합니다. 이로 인해 객체가 소멸되어도 WeakReference는 소멸되지 않습니다.
# WeakReference::__construct
WeakReference::__construct 메서드는 WeakReference 객체를 생성합니다. 이 메서드는 다음과 같은 매개변수를 받습니다.
* `$value`: 참조할 객체의 주소
# 예제
#hostingforum.kr
php
class Person {
public $name;
function __construct($name) {
$this->name = $name;
}
}
$person = new Person('John');
// WeakReference 객체를 생성합니다.
$weakReference = new WeakReference($person);
// 객체가 소멸되지 않는 경우
$person = null;
$person = $weakReference->get();
// 객체가 소멸된 경우
$person = null;
unset($person);
$person = $weakReference->get();
// 객체가 소멸되었는지 확인합니다.
if ($person === null) {
echo 'Person 객체가 소멸되었습니다.';
} else {
echo 'Person 객체가 소멸되지 않았습니다.';
}
위 예제에서, `$weakReference` 객체는 `$person` 객체를 참조합니다. `$person` 객체가 소멸되지 않는 경우, `$weakReference->get()` 메서드는 `$person` 객체를 반환합니다. 그러나 `$person` 객체가 소멸된 경우, `$weakReference->get()` 메서드는 `null`을 반환합니다.
# 참고
WeakReference는 PHP 7.4 버전부터 사용할 수 있습니다. 또한, WeakReference는 PHP의 GC (Garbage Collector)가 객체를 소멸할 때까지 객체를 참조하지 않습니다. 이로 인해 객체가 소멸되지 않습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.