라이브러리
[PHP] ReflectionClass::getProperties - 속성 가져오기
PHP ReflectionClass::getProperties 메소드는 클래스의 속성을 반환하는 메소드입니다. 이 메소드는 클래스의 모든 속성을 반환할 수 있지만, 일부 속성은 private 또는 protected 속성이기 때문에 접근할 수 없습니다.
ReflectionClass::getProperties 메소드의 사용법
ReflectionClass::getProperties 메소드를 사용하려면, 먼저 ReflectionClass 인스턴스를 생성해야 합니다. 이 인스턴스는 클래스의 이름을 사용하여 생성할 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스 이름');
속성 속성 반환
getProperties 메소드는 속성 이름과 속성 정보를 반환하는 배열을 반환합니다. 속성 정보는 속성 이름, 속성 타입, 속성 속성 등이 포함됩니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스 이름');
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
echo $property->getName() . ' : ' . $property->getValue($reflectionClass->newInstance()) . "
";
}
예제
#hostingforum.kr
php
class Person {
private $name;
protected $age;
public $email;
public function __construct($name, $age, $email) {
$this->name = $name;
$this->age = $age;
$this->email = $email;
}
}
$reflectionClass = new ReflectionClass('Person');
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
echo $property->getName() . ' : ' . $property->getValue($reflectionClass->newInstance()) . "
";
}
이 예제에서는 Person 클래스의 모든 속성을 반환합니다. 속성 이름과 속성 정보가 출력됩니다.
속성 정보 반환
속성 정보를 반환하려면, ReflectionProperty 메소드를 사용하여 속성 정보를 얻을 수 있습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('클래스 이름');
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
foreach ($properties as $property) {
echo $property->getName() . ' : ' . $property->getType() . ' : ' . $property->isPublic() . ' : ' . $property->isPrivate() . ' : ' . $property->isProtected() . "
";
}
이 예제에서는 속성 이름, 속성 타입, 속성 속성 등이 출력됩니다.
결론
ReflectionClass::getProperties 메소드는 클래스의 속성을 반환하는 메소드입니다. 이 메소드는 클래스의 모든 속성을 반환할 수 있지만, 일부 속성은 private 또는 protected 속성이기 때문에 접근할 수 없습니다. 속성 정보를 반환하려면, ReflectionProperty 메소드를 사용하여 속성 정보를 얻을 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.