
ReflectionClass::__toString() 메소드는 객체의 정보를 문자열로 반환하는 메소드입니다. 반환하는 문자열은 다음과 같은 정보를 포함합니다.
- 객체의 이름
- 객체의 부모 클래스 이름
- 객체의 인터페이스 이름
- 객체의 속성 이름과 타입
- 객체의 메소드 이름과 반환 타입
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function sayHello() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$reflection = new ReflectionClass('Person');
echo $reflection->__toString();
이 코드를 실행하면 다음과 같은 결과가 나타납니다.
#hostingforum.kr
php
Class [ class ReflectionClass ] {
- ReflectionClass::__construct() =
- (no methods)
- (no properties)
}
이 결과에서 볼 수 있듯이, ReflectionClass::__toString() 메소드는 객체의 이름, 부모 클래스 이름, 인터페이스 이름, 속성 이름과 타입, 메소드 이름과 반환 타입을 반환하지 않습니다. 대신, 객체의 클래스 이름과 속성, 메소드 정보를 반환하지 않습니다.
이 메소드는 ReflectionClass 객체의 정보를 반환하지 않습니다. 대신, ReflectionClass 객체를 문자열로 변환합니다.
만약 ReflectionClass 객체의 정보를 문자열로 반환하고 싶다면, ReflectionClass::getMethods(), ReflectionClass::getProperties() 메소드를 사용하여 객체의 메소드와 속성을 가져와 문자열로 변환할 수 있습니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function sayHello() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$reflection = new ReflectionClass('Person');
$methods = $reflection->getMethods();
$properties = $reflection->getProperties();
echo "Methods:n";
foreach ($methods as $method) {
echo "- $method->getName()n";
}
echo "nProperties:n";
foreach ($properties as $property) {
echo "- $property->getName()n";
}
이 코드를 실행하면 다음과 같은 결과가 나타납니다.
#hostingforum.kr
Methods:
- sayHello
Properties:
- name
- age
이 결과에서 볼 수 있듯이, ReflectionClass::getMethods(), ReflectionClass::getProperties() 메소드를 사용하여 객체의 메소드와 속성을 가져와 문자열로 변환할 수 있습니다.
2025-05-21 05:19