PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > PHP的闭包和异常处理--PHP培训十期线上班

PHP的闭包和异常处理--PHP培训十期线上班

Miss灬懒虫
Miss灬懒虫 原创
2020年02月17日 11:33:52 1318浏览

PHP的闭包和异常处理

学习总结

因为,闭包可以将一个执行环境(父级中的变量/状态)封装保存到匿名函数中。所以,闭包常作为 函数/方法 的回调参数来使用。

但是闭包的使用,不会释放父级中的变量/状态内存,所以要根据实际需要使用闭包;

其次,若是我们需要在闭包中,方位类中的私有/受保护成员的成员,我们可以同在类中创建一个包含闭包的方法来实现;

至于异常这一块,我们在代码编写过程中用的比较多,因为可以很好提升代码的健壮性,而我们为了是提示看起来更加美观与方便阅读,我们可以通过显示模板的形式来做。

但是需要注意一点,因为Exception类的构造方法,返回的字符串,所以我们想要使用模板文件,就要自定一个异常类,并且使用自定义的方法,将模板文件进行数据渲染加载才可以,不然会报错哦!

目录结构

  • BindUser.php :PHP闭包的应用实例;

  • ErrorMsg.html :异常提示的模板文件;

  • MyException.php :为了调用自定义异常显示模板,创建的脚本;

  • Validation.php :代码异常验证脚本;

运行效果

闭包的使用

异常处理提示

项目代码

BindUser.php

<?php/** * Created by PhpStorm. * User: Air15_2019 * Date: 2020/2/15 * Time: 14:31 *//** PHP中的闭包,使用匿名函数表示。与此同时,闭包也是一个Closure类的实例化对象;* 因为,闭包可以将一个执行环境(父级中的变量/状态)封装保存到匿名函数中。* 所以,闭包常作为 函数/方法 的回调参数来使用。* *//* * bindTo():复制当前闭包对象,绑定指定$this和类作用域(实例化方法) * bind():复制一个闭包,绑定指定$this和类作用域(静态方法) * _invoke(): 将闭包对象当成函数调用时触发(魔术方法) * __construct(void): 用来禁止外部实例化Closure类(构造方法) * */namespace Closures;//导入闭包类use Closure;class BindUsers{    public $name ='管理员';    private $id =1;    protected $email='admin@mayitiyu.cn';    public static $typeName='AntSports';    public function __toString()    {        // TODO: Implement __toString() method.        return $this->id.'<br>'.$this->name.'<br>'.$this->email.'<br>'.static::$typeName;    }    //通过自身类中的方法,修改私有或者受保护的类成员    final public function callClosure(int $id, string $email) {        //使用匿名函数闭包,返回值 匿名函数整体结果        $result=function (int $id, string $email):void {            $this->id=$id;            $this->email=$email.'@mayitiyu.cn';        };        //将闭包绑定到类上        $closure = Closure::bind($result, $this,BindUsers::class);        //将传入的参数赋给 $closure        $closure($id,$email);        return $closure;    }}//实例化一个对象$user=new BindUsers();//原始输出echo $user;echo '<hr>';/* * 创建一个闭包函数,在类的外部更新 公共或静态类成员的值 * */// 1.创建闭包函数$extClosure = function (string $name, string $typeName) : void{    $this->name = $name;    static::$typeName= $typeName;};// 1. 将闭包绑定到对象上, 返回一个新闭包对象,用来调用原闭包$closure = Closure::bind($extClosure,$user);$closure('项目部主管', '蚂蚁体育');//2.通过类中的方法,更新修改私有或者受保护的类成员的值$user->callClosure(78,'456');//打印输出echo $user;

MyException.php

<?php/** * Created by PhpStorm. * User: Air15_2019 * Date: 2020/2/15 * Time: 16:09 *///PHP可抛出的异常内容,请查询Exception类的类成员;namespace Closures;use Exception as ErrorMsg;class MyException extends ErrorMsg{    public function __toString()    {        // TODO: Change the autogenerated stub        parent::__toString();    }    //构造方法产生的值,自动被自定方法调用,加载到显示模板的HTML里面;    public function showError()    {        include __DIR__. '/ErrorMsg.html';    }}

ErrorMsg.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>错误异常提示</title></head><body><div class="tmp_message">    <table border="1" cellspacing="0" cellpadding="5">        <tr bgcolor="wheat">            <th>错误信息:</th>            <th>代码:</th>            <th>文件:</th>            <th>行号:</th>        </tr>        <tr>            <td><?=$this->message;?></td>            <td><?=$this->code;?></td>            <td><?=$this->file;?></td>            <td><?=$this->line;?></td>        </tr>    </table></div></body></html>

Validation.php

<?php/** * Created by PhpStorm. * User: Air15_2019 * Date: 2020/2/15 * Time: 16:01 */require 'MyException.php';use Closures\MyException;// 使用自定义的异常子类try {    $i=1;    //正在执行代码的错误判断,如果if条件为真,则输出 catch中的内容提示    if ($i===1) throw new MyException('邮箱或密码错误', 305);    echo '$i===1';} catch (MyException $myException) {    return $myException->showError();};
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议