Home > Backend Development > PHP Tutorial > 关于拦截器的功用

关于拦截器的功用

WBOY
Release: 2016-06-13 13:12:30
Original
1310 people have browsed it

关于拦截器的作用

class Computer {

private $_name;
private $_model;
private $_cpu;

private function __set($_key,$_value) {
$this->$_key = $_value;
}

private function __get($_key) {
return $this->$_key;
}

}


$computer = new Computer();
$computer->_name = '联想';
$computer->_cpu = '四核';
$computer->_model = 'i7';

echo $computer->_name;
echo $computer->_cpu;
echo $computer->_model;
我想问的事如果使用了_set和_get方法,那么类的私有属性就可以在外面任意使用赋值修改,那么和把属性设置为Public有何区别了,感觉上这两个方法没有什么用了。

------解决方案--------------------
是的,对于你的用法,的确是画蛇添足
------解决方案--------------------
set不是你那样用的,你去看哈其他开源框架就清楚了,特别是些开源框架的数据库Active Record类
------解决方案--------------------
面向对象就是要隐藏数据,开放接口。

你遵不遵循是你的事,程序只会听你的话,语法就这么回事。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template