라이브러리

[PHP] ReflectionClass::isIterateable - ReflectionClass::isIterable의 별칭




ReflectionClass::isIterateable


PHP 7.1 버전부터 ReflectionClass::isIterateable 메소드가 추가되었습니다. 이 메소드는 인스턴스 또는 클래스가 반복 가능한지 여부를 확인하는 데 사용됩니다.

# 반복 가능성


반복 가능성은 PHP에서 객체 또는 클래스가 foreach 문을 사용하여 반복될 수 있는지 여부를 의미합니다. 예를 들어, 배열은 반복 가능합니다. 그러나 객체는 반복 가능하지 않을 수 있습니다.

# ReflectionClass::isIterateable 메소드


ReflectionClass::isIterateable 메소드는 인스턴스 또는 클래스가 반복 가능한지 여부를 확인하는 데 사용됩니다. 이 메소드는 boolean 값을 반환하며, true 인 경우 인스턴스 또는 클래스가 반복 가능하고, false 인 경우 반복 불가능합니다.

# 예제


#hostingforum.kr
php

class Person {

    private $name;

    private $age;



    public function __construct($name, $age) {

        $this->name = $name;

        $this->age = $age;

    }



    public function getName() {

        return $this->name;

    }



    public function getAge() {

        return $this->age;

    }

}



class People {

    private $people;



    public function __construct() {

        $this->people = array();

    }



    public function addPerson(Person $person) {

        $this->people[] = $person;

    }



    public function getPeople() {

        return $this->people;

    }

}



$people = new People();

$people->addPerson(new Person('John', 30));

$people->addPerson(new Person('Jane', 25));



$reflectionClass = new ReflectionClass('People');

echo $reflectionClass->isIterateable() ? 'true' : 'false'; // true



$reflectionClass = new ReflectionClass('Person');

echo $reflectionClass->isIterateable() ? 'true' : 'false'; // false



# 결론


ReflectionClass::isIterateable 메소드는 인스턴스 또는 클래스가 반복 가능한지 여부를 확인하는 데 사용됩니다. 예제를 통해 반복 가능성에 대한 이해를 돕고, 이 메소드를 사용하는 방법을 보여줍니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 289 페이지

검색

게시물 검색