博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

私有字段private也可以外部访问

Posted on 2017-07-27 11:48  艾特水煮鱼  阅读(234)  评论(0编辑  收藏  举报
<?php
//私有字段private也可以外部访问
class nowamagic {
private $domain;
function __get($key){
return "使用get访问属性".$this->$key;
}
function __set($key,$value){
$this->$key = $value;
echo("使用set设置属性$key, 赋值为:<font color=red>$value</font>");
}
}

$ins = new nowamagic();
$ins->domain = "nowamagic.net";
echo '<br />';
echo $ins->domain;

//输出
//使用set设置属性domain, 赋值为:nowamagic.net
//使用get访问属性nowamagic.net



?>