পিএইচপি বন্ধুর মতো ঘোষণা সমর্থন করে না। এটি PHP5 এ __get এবং __set পদ্ধতি ব্যবহার করে এবং অনুমোদিত বন্ধু ক্লাসের জন্য একটি ব্যাকট্রেস পরিদর্শন করে সিমুলেট করা যেতে পারে। কিন্তু এই ধরনের কোডিং অনুশীলনকে আনাড়ি বলে মনে করা হয় −
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);
}
}