php之重载__get() __set() isset() unset() __call() __callStatic()
来源:互联网 发布:淘宝联盟没有佣金原因 编辑:程序博客网 时间:2024/05/19 18:45
php中的overloading跟传统面向对象的重写不同,例如java中:
class A{ public void methodName(参数1); public void methodName(参数1,参数2); public void methodName(参数1,参数2,参数3); ...}
php中重载是对类或者对象调用不存在的属性或方法时一种“优雅”的错误处理机制。想不明白为什么PHP这也叫重载,跟传统面向对象编程中的重载可以说是风马牛不相及。
php的重载依靠魔术方法__get() __set() isset() unset() __call() __callStatic()来实现,在我们访问不存在的属性或者方法时候,系统会自动调用这些魔术方法。
代码:
<?phpheader("content-type:text/html;charset=utf-8");class Sample{ public $p1 = 1;//类中声明的属性p1 //保存被重载的数据 private $data = array(); /* 在访问对象不存在的属性时__get被调用 $name:变量名 */ function __get($name) { "<br>__get:: ".$name; if(array_key_exists($name, $this->data)){ return $this->data[$name]; } $trace = debug_backtrace(); trigger_error( '访问类中不存在的属性'.$name. ' 文件:'.$trace[0]['file']. ' 所在行'.$trace[0]['line'] ,E_USER_WARNING); return null; } /* 对象不存在的属性赋值时__set被调用 $name:变量名 $value:变量值 */ function __set($name,$value){ echo "<br> __set:: $name = $value "; $this->data[$name] = $value; } /* 对象不存在的属性使用isset()或empty() 时__isset被调用 $name:变量名 $value:变量值 */ function __isset($name){ echo "<br>isset:: $name "; return isset($this->data[$name]); } /* 对象不存在的属性使用unset()时被调用 $name:变量名 $value:变量值 */ function __unset($name){ echo "<br>__unset:: $name"; unset($this->data[$name]); } /* 类的对象的不存在的实例方法调用的时候,会自动调用本方法,该方法必须是public */ public function __call($name , $arguments){ if($name==='f1'){ echo "<br>".$name."方法被调用,"."参数:"; var_dump($arguments); }else if($name==='f2'){ echo "<br>".$name."方法被调用,"."参数:"; var_dump($arguments); }else{ trigger_error("非法调用!",E_USER_WARNING); } } /* 类的对象的不存在的静态方法调用的时候,会自动调用本方法 5.3.0 新增 __callStatic()魔术方法。可见性未设置为 public 或未声明为 static 的时候会产生一个警告 */ public static function __callStatic($name , $arguments){ echo "<br>".$name."静态方法被调用,"."参数:"; var_dump($arguments); } }$s = new Sample();echo "<br>访问类中存在的实例属性:s->p1:: ".$s->p1;//属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 PHP 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。//Fatal error: Access to undeclared static property: Sample::$p3//echo "<br>访问类中不存在的静态属性不会调用魔术方法__get:".Sample::$p3;//错误//访问类中不存在的属性,类中的魔术方法__get会被调用echo $s->p2;//给类中不存在的属性赋值,类中的魔术方法__set会被调用$s->p2 = 88;echo '<br>'.$s->p2; //输出 88//类中的魔术方法__isset会被调用var_dump(isset($s->p2));//类中的魔术方法__isset会被调用var_dump(empty($s->p2));//类中的魔术方法__unset会被调用unset($s->p2);echo '<br>'.$s->p2; //p2被销毁,报错$s->f1();$s->f1(1,2);$s->f1(1,2,"hello");$s->f2(true,"111");$s->f3();//调用对象不存在的静态方法Sample::say('hello');Sample::say('hello','php');?>
0 0
- php之重载__get() __set() isset() unset() __call() __callStatic()
- PHP __set(),__get(),isset(),unset()函数
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __uns
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- php的魔术方法__get(),__set(),__call(),__callStatic()以及static延迟静态绑定
- PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP面向对象 __call() __isset() __unset __get() __set() 的用法
- PHP中__construct(), __destory(), __get(), __set(), __call(), __toString(), __clone
- PHP中__construct(), __destory(), __get(), __set(), __call(), __toString(), __clo
- PHP中__construct(), __destory(), __get(), __set(), __call(), __toString(), __clone()
- PHP 魔术方法之 __call 与 __callStatic
- bzoj 3684: 大朋友和多叉树 生成函数
- 图片加水印
- FireMonkey下类似手机QQ侧滑菜单的实现
- Java解析Json缺少jar报错:org/apache/commons/lang/exception/NestableRuntimeException
- hive jdbc程序开发
- php之重载__get() __set() isset() unset() __call() __callStatic()
- 剑指offer(十)之矩形覆盖
- xmpp开发IM即时通讯系列(二)--即时通讯服务器搭建(2)openfire服务器搭建
- 一个炫酷的SearchView搜索动画库
- 关于包络概念的理解
- vector使用方法
- Android_ListView
- Android WiFi调式工具WiFi ADB的使用
- python 按照cpu的使用率对top中的进程排序(排序表格)