라이브러리

[PHP] ReflectionClass::getProperty - 클래스 속성에 대한 ReflectionProperty를 가져옵니다.




ReflectionClass::getProperty


PHP의 ReflectionClass::getProperty 메소드는 클래스의 속성을 리플렉션하여 가져올 수 있는 메소드입니다. 리플렉션은 클래스의 구조를 분석하여 정보를 가져올 수 있는 기능입니다.

사용법


ReflectionClass::getProperty 메소드는 다음 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

ReflectionClass::getProperty($class, $propertyName)



* `$class`: 리플렉션할 클래스의 이름 또는 인스턴스
* `$propertyName`: 리플렉션할 속성의 이름

예제


다음 예제는 ReflectionClass::getProperty 메소드를 사용하여 클래스의 속성을 가져오는 방법을 보여줍니다.

#hostingforum.kr
php

// User 클래스 정의

class User {

    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;

    }

}



// ReflectionClass::getProperty 사용

$user = new User('John Doe', 30);

$reflectionClass = new ReflectionClass($user);

$propertyName = 'name';



// 속성 가져오기

$property = $reflectionClass->getProperty($propertyName);

echo $property->getName() . "
"; // John Doe



// 속성 읽기

echo $property->getValue($user) . "
"; // John Doe



// 속성 쓰기

$property->setValue($user, 'Jane Doe');

echo $user->getName() . "
"; // Jane Doe



속성 정보 가져오기


ReflectionClass::getProperty 메소드는 속성의 정보를 가져올 수 있는 메소드도 제공합니다. 다음 예제는 속성 정보를 가져오는 방법을 보여줍니다.

#hostingforum.kr
php

// 속성 정보 가져오기

$property = $reflectionClass->getProperty($propertyName);

echo $property->getName() . "
"; // name

echo $property->isPrivate() ? 'private' : 'public' . "
"; // private

echo $property->isReadable() ? 'readable' : 'un-readable' . "
"; // readable

echo $property->isWritable() ? 'writable' : 'un-writable' . "
"; // writable



결론


ReflectionClass::getProperty 메소드는 클래스의 속성을 리플렉션하여 가져올 수 있는 메소드입니다. 이 메소드는 속성의 정보를 가져올 수 있는 메소드도 제공하며, 속성의 읽기 및 쓰기 가능 여부를 확인할 수 있습니다. 이 메소드는 클래스의 구조를 분석하여 정보를 가져올 수 있는 기능인 리플렉션을 사용하여 클래스의 속성을 가져올 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색