PHP满载

WBOY
WBOYOriginal
2016-06-13 12:23:17895browse

PHP重载

PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的。属性的重载通过__set,__get,__isset,__unset来分别实现对不存在属性的赋值、读取、判断属性是否设置、销毁属性。

<strong style="background-color:rgb(255,255,255)">class Car {    private $ary = array();        public function __set($key, $val) {        $this->ary[$key] = $val;    }        public function __get($key) {        if (isset($this->ary[$key])) {            return $this->ary[$key];        }        return null;    }        public function __isset($key) {        if (isset($this->ary[$key])) {            return true;        }        return false;    }        public function __unset($key) {        unset($this->ary[$key]);    }}$car = new Car();$car->name = '汽车';  //name属性动态创建并赋&#20540;echo $car->name;</strong>

方法的重载通过__call来实现,当调用不存在的方法的时候,将会转为参数调用__call方法,当调用不存在的静态方法时会使用__callStatic重载。

<strong style="background-color:rgb(255,255,255)">class Car {    public $speed = 0;        public function __call($name, $args) {        if ($name == 'speedUp') {            $this->speed &#43;= 10;        }    }}$car = new Car();$car->speedUp(); //调用不存在的方法会使用重载echo $car->speed;</strong>

版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn