首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

php 魔术方法使用说明

一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!! PHP5.0后,php面向对象提成更多方法,使得php更加的强大!! 一些在PHP叫魔术方法的函数,在这里介绍一下:其实在一般的应用中,我们都需要用到他们!! 1.__construct() 当实例化一个对象的时候,这个对象的这个方法首先被调用。 Java代码 class Test { function __construct() { echo "before"; } } $t = new Test(); class Test { function __construct() { echo "before"; } } $t = new Test(); 输出是: start 我们知道php5对象模型 和类名相同的函数是类的构造函数,那么如果我们同时定义构造函数和__construct()方法的话,php5会默认调用构造函数而不会调用__construct()函数,所以__construct()作为类的默认的构造函数 2.__destruct() 当删除一个对象或对象操作终止的时候,调用该方法。 Java代码 class Test { function __destruct() { echo "end"; } } $t = new Test();将会输出end class Test { function __destruct() { echo "end"; } } $t = new Test();将会输出end 我们就可以在对象操作结束的时候进行释放资源之类的操作 3.__get() 当试图读取一个并不存在的属性的时候被调用。 如果试图读取一个对象并不存在的属性的时候,PHP就会给出错误信息。如果在类里添加__get方法,并且我们可以用这个函数实现类似java中反射的各种操作。 Java代码 class Test { public function __get($key) { echo $key . " 不存在"; } } $t = new Test(); echo $t->name; 就会输出:name 不存在 class Test { public function __get($key) { echo $key . " 不存在"; } } $t = new Test(); echo $t->name; 就会输出:name 不存在 4.__set() 当试图向一个并不存在的属性写入值的时候被调用。 Java代码 class Test { public function __set($key,$value) { echo '对'.$key . "附值".$value; } } $t = new Test(); $t->name = "aninggo"; 就会输出:对 name 附值 aninggo class Test { public function __set($key,$value) { echo '对'.$key . "附值".$value; } } $t = new Test(); $t->name = "aninggo"; 就会输出:对 name 附值 aninggo 5.__call() 当试图调用一个对象并不存在的方法时,调用该方法。 Java代码 class Test { public function __call($Key, $Args) { echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go); class Test { public function __call($Key, $Args) { echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go); 程序将会输出: Java代码 您要调用的 getName 方法不存在。参数是:Array ( [0] => aning [1] => go ) 您要调用的 getName 方法不存在。参数是:Array ( [0] => aning [1] => go ) 6.__toString() 当打印一个对象的时候被调用 这个方法类似于java的toString方法,当我们直接打印对象的时候回调用这个函数 class Test { public function __toString() { return "打印 Test"; } } $t = new Test(); echo $t; 运行ec

03

PHP基于Closure类创建匿名函数的方法详解

本文实例讲述了PHP基于Closure类创建匿名函数的方法。分享给大家供大家参考,具体如下: Closure 类 用于代表匿名函数的类。 匿名函数(在 PHP 5.3 中被引入)会产生这个类型的对象。在过去,这个类被认为是一个实现细节,但现在可以依赖它做一些事情。自 PHP 5.4 起,这个类带有一些方法,允许在匿名函数创建后对其进行更多的控制。 这个类不能实例化,里面主要有两个方法,都用来复制闭包,一个静态一个动态,下面分别详细讲解下这两个不好理解的方法。 Closure::bind public static Closure Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) 参数说明: closure 需要绑定的匿名函数。 newthis 需要绑定到匿名函数的对象,或者 NULL 创建未绑定的闭包。 newscope 想要绑定给闭包的类作用域,或者 'static' 表示不改变。如果传入一个对象,则使用这个对象的类型名。 类作用域用来决定在闭包中 $this 对象的 私有、保护方法 的可见性。 The class scope to which associate the closure is to be associated, or 'static' to keep the current one. If an object is given, the type of the object will be used instead. This determines the visibility of protected and private methods of the bound object. 上面是该方法的定义,第一个参数很好理解,就是一个闭包函数;第二个/【要记得博客地址www.isres.com】/参数就不太好理解,如果要复制的闭包中包含$this,这个对象就表示这个$this,闭包函数里面对这个对象的修改在调用结束之后也会保持一致,比如修改了一个属性;第三个参数就不太好理解了,看官方的说明也是云里雾里的,默认参数情况下,调用$this->访问object $newthis中的属性函数的时候,会有限制,只能访问public属性的函数,如果想访问protected/private属性,就要设置为对应的类名/类实例,就要像在类里面一样,要访问那个类的保护/私有属性函数。 例子

04
领券