라이브러리
[PHP] SplObjectStorage::offsetExists - 저장소에 개체가 있는지 확인합니다.
SplObjectStorage::offsetExists
`SplObjectStorage::offsetExists`는 SplObjectStorage 객체에서 특정 키가 존재하는지 확인하는 메서드입니다. 이 메서드는 SplObjectStorage 객체에서 특정 객체를 찾는 데 사용됩니다.
# 사용법
`offsetExists` 메서드는 다음과 같은 형식으로 사용됩니다.
#hostingforum.kr
php
bool SplObjectStorage::offsetExists ( object $object )
* `$object` : 찾으려는 객체
# 예제
#hostingforum.kr
php
// 객체를 생성합니다.
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
// SplObjectStorage 객체를 생성합니다.
$storage = new SplObjectStorage();
// 객체를 SplObjectStorage 객체에 추가합니다.
$person1 = new Person('John');
$storage->attach($person1);
// 객체를 SplObjectStorage 객체에 추가합니다.
$person2 = new Person('Jane');
$storage->attach($person2);
// offsetExists 메서드를 사용하여 객체가 존재하는지 확인합니다.
if ($storage->offsetExists($person1)) {
echo "Person 'John' exists in the storage.
";
} else {
echo "Person 'John' does not exist in the storage.
";
}
if ($storage->offsetExists($person2)) {
echo "Person 'Jane' exists in the storage.
";
} else {
echo "Person 'Jane' does not exist in the storage.
";
}
if ($storage->offsetExists(new Person('Bob'))) {
echo "Person 'Bob' exists in the storage.
";
} else {
echo "Person 'Bob' does not exist in the storage.
";
}
# 결과
#hostingforum.kr
Person 'John' exists in the storage.
Person 'Jane' exists in the storage.
Person 'Bob' does not exist in the storage.
# 설명
* `offsetExists` 메서드는 SplObjectStorage 객체에서 특정 객체를 찾는 데 사용됩니다.
* `$object` 매개변수에는 찾으려는 객체를 전달합니다.
* 메서드는 객체가 SplObjectStorage 객체에 존재하는지 확인하고, 존재하면 `true`를 반환하고, 존재하지 않으면 `false`를 반환합니다.
* 예제에서는 `Person` 클래스의 객체를 SplObjectStorage 객체에 추가하고, `offsetExists` 메서드를 사용하여 객체가 존재하는지 확인합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.