扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍了php中魔术方法是什么,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
陆良网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联公司自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
类中的魔术方法
PHP 魔术方法指的是在某些时刻会自动被调用的内置函数,它们以两个连续的下划线开头。
类的构造函数,用于初始化对象,在对象实例化时自动运行
析构函数,用于在 php 运行终止时,释放对象所占用的内存。析构函数是 php 的垃圾回收机制,使用栈结构,后进先出。
class computer{ private $brand; function __construct($brand){ $this->brand = $brand; } function __destruct(){ echo "release ".$this->brand."
"; } } $myComputer = new computer("MAC"); $yourComputer = new computer("Asus"); $hisComputer = new computer("Dell"); echo "end of php file
";
输出结果如下所示
end of php file release Dell release Asus release MAC
可以发现析构函数在 php 文件执行结束之后才执行
类中用 protected 和 private 关键字定义的成员属性或方法是无法通过对象的实例访问的。__get() 方法会且仅会在对象的实例访问 proctected 和 private 成员属性时自动执行 (访问成员方法时不会,因为没有意义)。
__get() 方法的意义在于将 proctected 和 private 成员属性进行处理后输出。
__get() 有且仅有一个输入参数
class computer{ private $brand; protected $owner; public $price; function __construct($brand, $owner, $price){ $this->brand = $brand; $this->owner = $owner; $this->price = $price; } function __get($name){ echo "It's up to me to decide if let you konw the owner and the brand of this computer or not :)
"; echo "I will tell you the name of woner: ".$this->owner."
"; echo "I won't tell you that the brand is ".md5($this->brand)."
"; echo "
"; } function __destruct(){ echo "release ".$this->brand."
"; } } $myComputer = new computer("MAC", "me", "1000"); $yourComputer = new computer("Asus", "you", "500"); $hisComputer = new computer("Dell", "his", "700"); echo $myComputer->price; echo "
"; echo $myComputer->owner; echo $yourComputer->brand; echo "end of php file
";
输出如下
1000 It's up to me to decide if let you konw the owner and the brand of this computer or not :) I will tell you the name of woner: me I won't tell you that the brand is 2e25c285356cbb0ed8785a1377027d79 It's up to me to decide if let you konw the owner and the brand of this computer or not :) I will tell you the name of woner: you I won't tell you that the brand is cb6ab3315634a1e4d11b091ba48b60ba end of php file release Dell release Asus release MAC
可以看到,当访问 public 成员属性 price 时,__get()方法并没有被调用。输出 brand 时,我们使用了 md5 对其进行了加密处理,这种对封装的成员属性进行处理后输出的用法就是 get 方法的意义所在。
__set($name, $value) 与用于给当前类中封装的方法或属性进行重新赋值或定义。
与 get 类似但不同的时,__set($name, $value)会在成员属性被访问赋值时自动执行,其中 $name 是被访问的成员属性名,$value 为成员属性被赋予的值
class computer{ private $brand; protected $owner; function __construct($brand, $owner, $price){ $this->brand = $brand; $this->owner = $owner; $this->price = $price; } function __get($name){ echo "It's up to me to decide if let you konw the owner and the brand of this computer or not :)
"; echo "I will tell you the name of woner: ".$this->owner."
"; echo "I won't tell you that the brand is ".md5($this->brand)."
"; echo "
"; } function __set($name, $value){ $this->owner = $value; echo "set $name to $value"."
"; } function __destruct(){ echo "release ".$this->brand."
"; } } $myComputer = new computer("MAC", "me", "1000"); echo $myComputer->owner = "my friend"; echo $myComputer->owner; echo "end of php file
";
输出结果
set owner to my friend my friendIt's up to me to decide if let you konw the owner and the brand of this computer or not :) I will tell you the name of woner: my friend I won't tell you that the brand is 2e25c285356cbb0ed8785a1377027d79 end of php file release MAC
我们看到在给 owner 赋值时调用了 set , 而访问属性时,调用了 get 。
用于直接打印对象句柄,也就是说当我们使用 echo 加对象名时,__torsring()将会被自动调用
class computer{ function __tostring(){ return "This is a computer class"; } } $myComputer = new computer(); echo $myComputer;
如果没有 __totring() 方法,我们是无法使用 echo+对象名,会出现 fatal error
当我们调用不存在的方法时,__call() 会自动执行,用于进行异常处理,并使程序继续正常运行。
class computer{ function start(){ echo "starting computer
"; } function __call($m, $a){ echo "erro function: ".$m; echo "
"; echo "error param: "; print_r($a); echo "
"; } } $myComputer = new computer(); $myComputer->start(); $myComputer->shutdown('10 min', '20 min'); echo "here";
输出结果为
starting computer erro function: shutdown error param: Array ( [0] => 10 min [1] => 20 min ) here
我们可以看到,$method 返回了错误的函数名,而 arguments 返回了参数,最后输出了 "here" 说明程序继续正常运行。
clone 关键字用于复制对象,__clone() 方法实在克隆对象时自动调用的函数
class computer{ public $name; function __clone(){ echo "A computer has been cloned
"; } } $myComputer = new computer(); $youComputer = $myComputer; $youComputer->name = 'pc1'; echo "My computer's name is ".$myComputer->name."
"; echo "
"; $hisComputer = clone $myComputer; $hisComputer->name = 'pc2'; echo "My computer's name is ".$myComputer->name."
"; echo "His computer's name is ".$hisComputer->name."
";
输出结果
My computer's name is pc1 A computer has been cloned My computer's name is pc1 His computer's name is pc2
我们看到用 = 号并不能复制对象,只是为对象添加了一个别名而已,这里 $myComputer 和 $youComputer 指向同一块内存,修改了 $youComputer 的值相当于修改了 $myComputer 的值。
在实例化对象时,__autolaod() 会自动被调用,用于快速取得对应的类文件
带 try, catch 异常处理的例子
function __autoload($class_name){ echo "want to load ".$class_name."
"; if(file_exists($class_name.".class.php")){ include($class_name.".class.php"); }else{ throw new Exception("Unable to laod ".$class_name.".class.php"); } } try{ $obj = new myClass(); } catch(Exception $e){ echo $e->getMessage()."
"; }
感谢你能够认真阅读完这篇文章,希望小编分享的“php中魔术方法是什么”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流