라이브러리
[PHP] ReflectionReference::__construct - 직접 인스턴스화를 허용하지 않는 개인 생성자
ReflectionReference::__construct
PHP 7.4 버전부터 ReflectionReference 클래스가 추가되었습니다. 이 클래스는 PHP의 객체 참조를 표현하는 데 사용됩니다. ReflectionReference 클래스의 생성자인 `__construct`는 객체 참조를 초기화하는 데 사용됩니다.
ReflectionReference::__construct의 매개변수
`__construct` 메서드는 다음 매개변수를 받습니다.
* `$object`: 참조할 객체
* `$property`: 참조할 객체의 속성 (선택 사항)
예제
#hostingforum.kr
php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person('John Doe', 30);
$reflection = new ReflectionClass($person);
$property = $reflection->getProperty('name');
$reference = new ReflectionReference($person, $property);
echo $reference->getValue(); // John Doe
예제 설명
위 예제에서, `Person` 클래스의 인스턴스를 생성하고 `ReflectionClass`를 사용하여 객체를 분석합니다. `getProperty` 메서드를 사용하여 `name` 속성을 얻은 후, `ReflectionReference`를 사용하여 객체의 속성을 참조합니다. `getValue` 메서드를 사용하여 참조된 속성의 값을 얻을 수 있습니다.
추가 예제
#hostingforum.kr
php
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$person = new Person('Jane Doe', 25);
$reflection = new ReflectionClass($person);
$reference = new ReflectionReference($person);
echo $reference->getValue(); // Person Object ( [name] => Jane Doe [age] => 25 )
$reference->setValue('John Doe', 30);
echo $person->name; // John Doe
echo $person->age; // 30
추가 예제 설명
위 예제에서, `Person` 클래스의 인스턴스를 생성하고 `ReflectionReference`를 사용하여 객체를 참조합니다. `getValue` 메서드를 사용하여 참조된 객체의 값을 얻을 수 있습니다. `setValue` 메서드를 사용하여 참조된 객체의 속성을 변경할 수 있습니다. 변경된 속성의 값을 얻기 위해 `getValue` 메서드를 사용할 수 있습니다.
참고
`ReflectionReference` 클래스는 PHP 7.4 버전부터 사용할 수 있습니다. 이전 버전의 PHP에서는 사용할 수 없습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.