라이브러리

[PHP] ReflectionProperty::__toString - 문자열로




ReflectionProperty::__toString


PHP의 ReflectionProperty 클래스는 PHP 클래스의 속성을 반영하는 클래스입니다. ReflectionProperty 클래스는 속성의 이름, 타입, 접근 제어자, 속성의 값 등을 가져올 수 있습니다.

ReflectionProperty::__toString 메소드는 ReflectionProperty 객체를 문자열로 변환하는 메소드입니다. 이 메소드는 ReflectionProperty 객체의 정보를 문자열로 반환합니다.

예제


#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;

    }

}



$person = new Person('John Doe', 30);



$reflectionClass = new ReflectionClass('Person');

$reflectionProperty = $reflectionClass->getProperty('name');



echo $reflectionProperty->__toString() . "
"; // Output: "Property [ Person::$name ]"



echo $reflectionProperty->getName() . "
"; // Output: "name"

echo $reflectionProperty->getValue($person) . "
"; // Output: "John Doe"



위 예제에서, ReflectionProperty::__toString 메소드는 ReflectionProperty 객체를 문자열로 변환합니다. 이 문자열에는 속성의 이름, 타입, 접근 제어자, 속성의 값 등이 포함됩니다.

속성 정보 가져오기


ReflectionProperty 클래스는 속성의 정보를 가져올 수 있습니다. 다음은 속성의 정보를 가져오는 예제입니다.

#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;

    }

}



$person = new Person('John Doe', 30);



$reflectionClass = new ReflectionClass('Person');

$reflectionProperty = $reflectionClass->getProperty('name');



echo "속성 이름: " . $reflectionProperty->getName() . "
";

echo "속성 타입: " . $reflectionProperty->getType() . "
";

echo "속성 접근 제어자: " . $reflectionProperty->isPrivate() ? "private" : ($reflectionProperty->isProtected() ? "protected" : "public") . "
";

echo "속성 값: " . $reflectionProperty->getValue($person) . "
";



위 예제에서, ReflectionProperty 클래스는 속성의 이름, 타입, 접근 제어자, 속성의 값을 가져옵니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 207 페이지

검색

게시물 검색