从php5以后的版本,类就可以使用魔术方法了。php规定以两个下划线(__)开头的方法都保留为魔术方法,所以建议大家函数名最好不用__开 头,除非是为了重载已有的魔术方法。
目前php已有的魔术方法有 __construct,__destruct,__call,__get,__set,__isset,__unset,__sleep,__wakeup,__tostring,__set_state 和 __clone。
本节将讲讲__construct,__destruct:
__construct()- 在每次创建新对象时先调用此方法
__destruct() - 对象的所有引用都被删除或者当对象被显式销毁时执行
结果:
resource(2) of type (mysql link)resource(2) of type (unknown)
以上就介绍了php 魔术方法:__construct __destruct,包括了php 魔术方法方面的内容,希望对php教程有兴趣的朋友有所帮助。