概述
在面向对象编程中,PHP提供了一系列的魔术方法,这些魔术方法为编程提供了很多便利。PHP中的魔术方法通常以__(两个下划线)开始,并且不需要显示的调用而是由某种特定的条件出发。
开始之前
在总结PHP的魔术方法之前先来定义两个类,以便后边示例使用:
class Device{
public $name,$battery,$data = [],$connection;
protected function connect(){
$this->connection = 'resource';
echo $this->name.'connected'.PHP_EOL;
}
protected function disconnect(){
$this->connection = null;
echo $this->name.'disconnected'.PHP_EOL;
}
}
Device类有四个成员属性和两个成员方法。
class Battery{
private $charge = 0;
public function setCharge($charge){
$charge = (int)$charge;
if($charge < 0){
$charge = 0;
}else if($charge > 100){
$charge = 100;
}
$this->charge = $charge;
}
}
Battery类有一个成员属性和一个成员方法。
构造函数和析构函数
构造函数和析构函数分别在对象创建和销毁时被调用。对象被“销毁”是指不存在任何对该对象的引用,比如引用该对象的变量被删除(unset)、重新赋值或脚本执行结束,都会调用析构函数。
__construct()
__construct()构造函数是目前为止最经常使用的函数。在创建对象时,可以在构造函数中做一些初始化工作。可以为构造函数定义任意多个参数,只要在实例化时传入对应个数的参数即可。构造函数中出现的任何异常都会阻止对象的创建。
class Device{
public $name,$battery,$data = [],$connection;
public function __construct(Battery $battery,$name){
$this->battery = $battery;
$this->name = $name;
$this->connect();
}
protected function connect(){
$this->connection = 'resource';
echo $this->name.'connected'.PHP_EOL;
}
protected function disconnect(){
$this->connection = null;
echo $this->name.'disconnected'.PHP_EOL;
}
}
上面的示例代码中,Device类的构造函数为成员属性赋值并且调用了connect()方法。
将构造函数声明为私有方法,可以防止在类外部创建对象,这在单利模式中经常使用。
__desctruct()
析构函数通常在对象被销毁时调用,析构函数不接收任何参数。经常在析构函数中执行一些清理工作,比如关闭数据库连接等。
__get()
魔术方法__get()在我们尝试访问一个不存在的属性时会被调用。它接收一个参数,该参数表示访问属性的名字,并且将该属性的值返回。在上面的Device类里,有一个data属性,该属性就在这里就起了作用,如下面得代码:
class Device{
public $name,$battery,$data = [],$connection;
public function __construct(Battery $battery,$name){
$this->battery = $battery;
$this->name = $name;
$this->connect();
}
protected function connect(){
$this->connection = 'resource';
echo $this->name.'connected'.PHP_EOL;
}
protected function disconnect(){
$this->connection = null;
echo $this->name.'disconnected'.PHP_EOL;
}
}
$battery = new Battery();
$device = new Device($battery,'mac');
echo $device->aaa; //Notice: Undefined property: Device::$aaa
header("Content-type: text/html; charset=utf-8");
class Device{
public $name,$battery,$data = [],$connection;
public function __construct(Battery $battery,$name){
$this->battery = $battery;
$this->name = $name;
$this->connect();
}
public function __get($name){
if(array_key_exists($name,$this->data)){
return $this->data[$name];
}
return '属性不存在';
}
protected function connect(){
$this->connection = 'resource';
echo $this->name.'connected'.PHP_EOL;
}
protected function disconnect(){
$this->connection = null;
echo $this->name.'disconnected'.PHP_EOL;
}
}
$battery = new Battery();
$device = new Device($battery,'mac');
echo $device->aaa; //macconnected 属性不存在
该魔术方法最常用的地方就是通过创建一个“只读”的属性来扩展访问控制。在上面的Battery类中,有一个私有属性$charge,我们可以通过__get()魔术方法将该属性扩展为在类外部可读但不能修改。代码如下:
class Battery {
private $charge = 0;
public function __get($name) {
if(isset($this->$name)) {
return $this->$name;
}
return null;
}
}
__set()
__set()魔术方法在我们尝试修改一个不可访问的属性时会被调用,它接收两个参数,一个表示属性的名字,一个表示属性的值。示例代码如下:
header("Content-type: text/html; charset=utf-8");
class Device{
public $name,$battery,$data = [],$connection;
public function __construct(Battery $battery,$name){
$this->battery = $battery;
$this->name = $name;
$this->connect();
}
public function __get($name){
if(array_key_exists($name,$this->data)){
return $this->data[$name];
}
return '属性不存在';
}
public function __set($name,$value){
$this->data[$name] = $value;
}
protected function connect(){
$this->connection = 'resource';
echo $this->name.'connected'.PHP_EOL;
}
protected function disconnect(){
$this->connection = null;
echo $this->name.'disconnected'.PHP_EOL;
}
}
$battery = new Battery();
$device = new Device($battery,'mac');
$device->aaa = '哈哈';
echo $device->aaa; //macconnected 哈哈
__isset()
__isset()魔术方法在对一个不可访问的属性调用isset()方法时会被调用,它接收一个参数,表示属性的名字。它应该返回一个布尔值,用来表示该属性是否存在。代码如下:
class Device{
private function __isset($name){
return array_key_exists($name,$this->data);
}
如果对象里面成员是公有的,可以直接使用 isset() 函数。如果是私有的成员属性,那就需要在类里面加上一个 __isset() 方法
__unset()
__unset()魔术方法在调用unset()函数销毁一个不能访问的属性时会被调用,它接收一个参数,表述属性的名字。
PHP 中 16 个魔术方法详解
PHP 中 16 个魔术方法详解 前言 PHP中把以两个下划线__开头的方法称为魔术方法(Magic methods),这些方法在PHP中充当了举足轻重的作用. 魔术方法包括: __constru ...
php中的常用魔术方法总结
以下是对php中的常用魔术方法进行了详细的总结介绍,需要的朋友可以过来参考下 常用的魔术方法有:__Tostring () __Call() __autoLoad() __ clone() __GET ...
php面向对象类中常用的魔术方法
php面向对象类中常用的魔术方法 1.__construct():构造方法,当类被实例化new $class时被自动调用的方法,在类的继承中可以继承与覆盖该方法,例: //__construct( ...
PHP中常见魔术方法解析
PHP几种常见魔术方法与魔术变量解析