
ReflectionParameter::canBePassedByValue 메서드는 PHP 7.0.0 부터 지원되는 메서드입니다. 이 메서드는 객체를 값으로 전달할 수 있는지 여부를 확인하는 메서드입니다.
객체의 타입이 인터페이스인지, 클래스인지 여부에 따라 결정되는 것이 아닙니다. 대신, 객체가 Serializable 인터페이스를 구현하고 있는지 여부에 따라 결정됩니다.
Serializable 인터페이스를 구현한 객체는 직렬화(serialize)와 역직렬화(unserialize)가 가능하므로, 이러한 객체를 값으로 전달할 수 있습니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
class Person implements Serializable {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function serialize() {
return serialize($this->name);
}
public function unserialize($serialized) {
$this->name = unserialize($serialized);
}
}
$person = new Person('John');
// ReflectionParameter::canBePassedByValue 메서드는 true를 반환합니다.
var_dump(ReflectionParameter::canBePassedByValue(new ReflectionParameter('Person', 'person')));
위 코드에서 Person 클래스는 Serializable 인터페이스를 구현하고 있으므로, ReflectionParameter::canBePassedByValue 메서드는 true를 반환합니다.
2025-08-04 09:49