PHP는 friend-like 선언을 지원하지 않습니다. __get 및 __set 메서드를 사용하고 허용된 friend 클래스에 대한 역추적을 검사하여 PHP5에서 시뮬레이션할 수 있습니다. 그러나 이러한 유형의 코딩 관행은 서투른 것으로 간주됩니다 -
class sample_friend {
private $__friends = array('My_Friend', 'Other_Friend');
public function __get($key) {
$trace = debug_backtrace();
if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {
return $this->$key;
}
// __get() code goes here
trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);
}
public function __set($key, $value) {
$trace = debug_backtrace();
if(isset($trace[1]['class']) && in_array($trace[1]['class'], $this->__friends)) {
return $this->$key = $value;
}
// normal __set() code goes here
trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);
}
}