PHP 魔术方法之__set() __get() 方法
来源:互联网 发布:个人网站源码 编辑:程序博客网 时间:2024/05/03 09:56
先看看官方文档的解释
__set() is run when writing data to inaccessible properties.__get() is utilized for reading data from inaccessible properties.
究竟用中文怎么翻译呢? inaccessible :n. 难达到;难接近;无法理解。
有代码有真相:
<?phperror_reporting(E_ALL);class stu{ private $a; private $b = 0; public $c; public $d = 0; //这里的 private 可以用 protected public 替代 private function __get($name) { return 123; } //这里的 private 也可以用 protected public 替代 private function __set($name, $value) { echo "This is set function"; }}$s = new stu();var_dump($s->a); //output: 123var_dump($s->b); //output: 123var_dump($s->c); //output: nullvar_dump($s->d); //output: 0var_dump($s->e); //output: 123$s->a = 3; //output: This is set function$s->c = 3; //no output$s->f = 3; //output: This is set function?>
结果:
如果没有__get 方法,执行 var_dump(
如果没有__get 方法,执行 var_dump(
总结:
- 从一个难以访问的属性读取数据的时候 __get() 方法被调用
- 向一个难以访问的属性赋值的时候 __set() 方法被调用
- 难以访问包括:(1)私有属性,(2)没有初
参考网址:
http://www.cnblogs.com/glory-jzx/archive/2012/05/23/2514173.html
0 0
- PHP 魔术方法之__set() __get() 方法
- PHP魔术方法之__set()和__get()
- PHP 魔术方法之"__set()"和"__get"
- php 魔术方法 __set __get
- PHP魔术方法之__get()和__set()方法
- 魔术方法:__set、__get
- PHP 魔术方法__set() __get() 方法
- PHP 魔术方法__set() __get() 方法详解
- PHP 魔术方法__set() __get() 方法
- PHP 魔术方法__set() __get() 方法
- php魔术方法汇总之__set(),__get(),__isset (),__unset()
- php魔术方法__get和__set详解
- 魔术方法__set()和__get()
- php魔术方法__set和__get,__isset,__unset的用法
- 详解php的魔术方法__get()和__set()使用介绍
- php魔术方法__SET 和 __GET 应用实例
- php的魔术方法__get()、__set()、__isset()、__unset()的用法
- 面向对象之魔术方法__get()和__set()
- 白话经典算法系列之二 直接插入排序的三种实现
- 通读cheerio API
- 自旋锁-概念
- LintCode 16 带重复元素的排列
- Codeforces Round #398 (Div. 2) C. Garland
- PHP 魔术方法之__set() __get() 方法
- 程序员是怎样练成的?
- Python的简单输出
- 6访问权限控制
- Android之解析Json
- js传中文参数到服务端变成乱码问题
- VCC、VDD、VEE、VSS的区别
- bower
- HDU2416 Treasure of the Chimp Island ,bfs+优先队列