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 新年恋人祝福语简短创意