php魔术方法总结使用

(1)_clone克隆
class Weather{
    public $isRain = "要下雨了<br>";
    public function howIsWeather(){
        echo "明天要下雨哦<br>";
    }
}

$nextDay = new Weather;

$otherDay = $nextDay;

echo $otherDay->isRain;  //使用对象调用属性,也不加$,与$this->调用相同
$otherDay->howIsWeather();

实例化对象的赋值是传递的对象的地址索引,两个变量指向同一个对象,属于浅拷贝,而clone与其含义一样,是创建一个独立的相同的对象,与之前的模板对象没有关系

class Weather{
    public $isRain = "要下雨了<br>";
    public function howIsWeather(){
        echo "明天要下雨哦<br>";
    }
}

$nextDay = new Weather;

$otherDay = clone $nextDay;

echo $otherDay->isRain;
$otherDay->howIsWeather();
$otherDay->isRain = "不会下雨了";
echo $nextDay->isRain;   //并不会被修改

retrieve 取回

(2)__call()

实例调用不存在的方法时会调用__call()魔术方法,并将不存在函数的方法名与参数传入call

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容