PHP等同于朋友或内部
PHP不支持类似朋友的声明。可以在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()代码在这里
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;
}
//正常的__set()代码在这里
trigger_error('Cannot access private property ' . __CLASS__ . '::$' . $key, E_USER_ERROR);
}
}热门推荐
10 送蛇的祝福语简短
11 给师父春节祝福语简短
12 班级集体生日祝福语简短
13 送笔筒的祝福语简短
14 比较简短浪漫的祝福语
15 怎么说祝福语简短
16 天气转凉祝福语简短
17 怎么生孩子祝福语简短
18 5岁宝宝祝福语简短