라이브러리
[PHP] SplObjectStorage::offsetUnset - 저장소에서 객체를 제거합니다.
SplObjectStorage::offsetUnset
SplObjectStorage는 PHP의 내장 클래스 중 하나로, 객체를 저장하고 관리하는 데 사용됩니다. SplObjectStorage::offsetUnset 메소드는 SplObjectStorage 객체에서 특정 객체를 삭제하는 데 사용됩니다.
# offsetUnset 메소드의 사용법
offsetUnset 메소드는 SplObjectStorage 객체에서 특정 객체를 삭제합니다. 이 메소드는 객체의 참조를 삭제합니다. 객체 자체는 삭제되지 않습니다.
# 예제
#hostingforum.kr
php
class Person {
public $name;
function __construct($name) {
$this->name = $name;
}
}
$storage = new SplObjectStorage();
$person1 = new Person("John");
$person2 = new Person("Jane");
$storage->attach($person1);
$storage->attach($person2);
print("storage count: " . $storage->count() . "
"); // storage count: 2
$storage->offsetUnset($person1);
print("storage count: " . $storage->count() . "
"); // storage count: 1
위 예제에서, `offsetUnset` 메소드는 `$person1` 객체를 삭제합니다. `$storage` 객체의 개수는 1이 됩니다.
# 주의
`offsetUnset` 메소드는 객체의 참조를 삭제합니다. 객체 자체는 삭제되지 않습니다. 만약 객체가 다른 곳에서 참조되고 있으면, `offsetUnset` 메소드는 객체를 삭제하지 못할 수 있습니다.
# 예제 (객체가 다른 곳에서 참조되고 있는 경우)
#hostingforum.kr
php
class Person {
public $name;
function __construct($name) {
$this->name = $name;
}
}
$storage = new SplObjectStorage();
$person1 = new Person("John");
$person2 = new Person("Jane");
$storage->attach($person1);
$person2->friend = $person1;
print("storage count: " . $storage->count() . "
"); // storage count: 1
$storage->offsetUnset($person1);
print("storage count: " . $storage->count() . "
"); // storage count: 1
위 예제에서, `$person2` 객체는 `$person1` 객체를 참조하고 있습니다. `$storage` 객체에서 `$person1` 객체를 삭제하려고 시도했지만, `$person2` 객체가 `$person1` 객체를 참조하고 있기 때문에 `$person1` 객체는 삭제되지 않습니다.
결론
SplObjectStorage::offsetUnset 메소드는 SplObjectStorage 객체에서 특정 객체를 삭제하는 데 사용됩니다. 이 메소드는 객체의 참조를 삭제합니다. 객체 자체는 삭제되지 않습니다. 만약 객체가 다른 곳에서 참조되고 있으면, `offsetUnset` 메소드는 객체를 삭제하지 못할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.