首页 > 编程 > PHP > 正文

PHP中的常见魔术方法功能作用及用法

2020-03-22 19:14:02
字体:
来源:转载
供稿:网友
这篇文章主要介绍了关于PHP中的常见魔术方法功能作用及用法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

 

概述

html' target='_blank'>面向对象编程中,PHP提供了一系列的魔术方法,这些魔术方法为编程提供了很多便利。PHP中的魔术方法通常以__(两个下划线)开始,并且不需要显示的调用而是由某种特定的条件出发。

开始之前

在总结PHP的魔术方法之前先来定义两个类,以便后边示例使用:

 ?phpclass 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类有四个成员属性和两个成员方法。

 ?php 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()构造函数是目前为止最经常使用的函数。在创建对象时,可以在构造函数中做一些初始化工作。可以为构造函数定义任意多个参数,只要在实例化时传入对应个数的参数即可。构造函数中出现的任何异常都会阻止对象的创建。

 ?phpclass 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属性,该属性就在这里就起了作用,如下面得代码:

 ?php 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- //Notice: Undefined property: Device::$aaa
 ?phpheader( 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- //macconnected 属性不存在

该魔术方法最常用的地方就是通过创建一个“只读”的属性来扩展访问控制。在上面的Battery类中,有一个私有属性$charge,我们可以通过__get()魔术方法将该属性扩展为在类外部可读但不能修改。代码如下:

 ?php class Battery { private $charge = 0;  public function __get($name) {  if(isset($this- $name)) {  return $this- $name; return null;}

__set()

__set()魔术方法在我们尝试修改一个不可访问的属性时会被调用,它接收两个参数,一个表示属性的名字,一个表示属性的值。示例代码如下:

 ?phpheader( 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- //macconnected 哈哈        

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表