陈大剩博客

PHP匿名函数与匿名类详解

  • 陈大剩
  • 2020-08-25 23:09:48
  • 3454

匿名函数

引用官方的一句话:匿名函数(Anonymous functions),也叫闭包函数(closures),允许临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。

但是在PHP内,由于匿名函数是通过闭包类实现的( Anonymous functions are implemented using the Closure class),因此多数人混淆了闭包技术和匿名函数;实际上,闭包是一项「技术」或者说「功能」,能够捕获并存储当前当前上下文状态,以供后续使用。 匿名函数就只是一个「函数」,一个没有名字的函数而已。在实际应用中,匿名函数通常伴随着使用闭包技术;但闭包并不一定只能用在匿名函数内。在其他语言内是完全不同的两个概念的(记得区分闭包和匿名函数,匿名函数==闭包)。

匿名函数的形式

// 简单的匿名函数
$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};
$greet('ChenDasheng');
$greet('World');

//继承外部变量$message的简单的匿名函数
$message = 'Hello';
$example = function () use ($message) { //继承$message = 'Hello';
    var_dump($message);
};
echo $example();

常见的几种匿名函数用法(有很多中用法,列举比较常见的)

//匿名函数当作参数传递(Laravel中where闭包就是这种)
function myFunction($anonymousFunc){
    $anonymousFunc("Chen Dasheng");
}

myFunction(function($username){
    echo $username;
});

//在普通函数中返回匿名函数(比较常用)
function closureFunction(){
    $username = 'ChenDasheng';
    $anonymousFunc = function() use($username){
        echo $username;
    };
    return $anonymousFunc;// 函数返回匿名函数
}
$func = closureFunction();
$func(); //然后调用$func() 

//返回匿名函数并给匿名函数传参
function closureFunc(){
      $username = 'ChenDasheng';
      $anonymousFunc = function($lover,$skill) use($username){
          echo $username.$lover.$skill;
      };
      return $anonymousFunc;
  }
$func = closureFunc();
$func("喜欢","写代码");//ChenDasheng喜欢写代码

PHP预定义接口 Closure类

Closure {
    // 禁止实例化
    private __construct(void){}
    //复制一个闭包,绑定指定的 $newThis 对象和类的作用域
    public static bind( Closure $closure, object $newthis[, mixed $newscope  = 'static'] ){}
    //复制当前闭包对象, 绑定指定的 $newThis 对象和类的作用域 
    public bindTo( object $newthis[, mixed $newscope  = 'static'] ){}
}

Closure::bind 与 Closure::tobind (就是省略了一个参数)

class Person {
    private static $name = 'ChenDasheng';
    private $age = 25;
    public $sex='男';
}
$cl1 = static function() {
    return Person::$name;
};
$cl2 = function() {
    return $this->age;
};

$cl3 = function() {
    return $this->sex;
};
/**
 * Closure class a method
 * 复制一个闭包,绑定指定的 $newThis对象和类的作用域
 * @param Closure $closure 必填 表示闭包函数
 * @param object $newThis 必填 闭包中 $this 所指的对象 传入类名代表当前类,静态方法不能传值
 * @param mixed $newscope 可选 我们闭包中需要操作属性等所属类的类型名 默认是static,static只能取公共值
 * @return \Closure
 */
//output:ChenDasheng
$bcl1 = Closure::bind($cl1, null, 'Person');
//output:PHP Fatal error
$bcl1 = Closure::bind($cl1, new Person(), 'Person');
//output:25
$bcl2 = Closure::bind($cl2, new Person(), 'Person');
//output:PHP Fatal error
$bcl2 = Closure::bind($cl2, null, 'Person');
//output:PHP Fatal error
$bcl2 = Closure::bind($cl2, new Person());
//output:男
$bcl3 = Closure::bind($cl3, new Person());

匿名类

PHP 7 开始支持匿名类。 匿名类很有用,可以创建一次性的简单对象

// PHP 7 之前的代码
class Logger
{
    public function log($msg)
    {
        echo $msg;
    }
}

$util->setLogger(new Logger());

// 使用了 PHP 7+ 后的代码
$util->setLogger(new class {
    public function log($msg)
    {
        echo $msg;
    }
}); 

可以传递参数到匿名类的构造器,也可以扩展(extend)其他类、实现接口(implement interface),以及像其他普通的类一样使用 trait:

class SomeClass {}
interface SomeInterface {}
trait SomeTrait {}

var_dump(new class(10) extends SomeClass implements SomeInterface {
    private $num;

    public function __construct($num)
    {
        $this->num = $num;
    }

    use SomeTrait;
}); 

匿名类被嵌套进普通 Class 后,不能访问这个外部类(Outer class)的 private(私有)、protected(受保护)方法或者属性。为了访问外部类(Outer class)protected 属性或方法,匿名类可以 extend(扩展)此外部类。为了使用外部类(Outer class)的 private 属性,必须通过构造器传进来

class Outer
{
    private $prop = 1;
    protected $prop2 = 2;

    protected function func1()
    {
        return 3;
    }

    public function func2()
    {
        return new class($this->prop) extends Outer {
            private $prop3;

            public function __construct($prop)
            {
                $this->prop3 = $prop;
            }

            public function func3()
            {
                return $this->prop2 + $this->prop3 + $this->func1();
            }
        };
    }
}

echo (new Outer)->func2()->func3();  //output:6

参考

欢迎各位大佬补充;

分享到:
2

说点儿什么吧

头像

表情

本站由陈大剩博客程序搭建 | 湘ICP备2023000975号| Copyright © 2017 - 陈大剩博客 | 本站采用创作共用版权:CC BY-NC 4.0

站长统计| 文章总数[109]| 评论总数[9]| 登录用户[22]| 时间点[112]

logo

登入

社交账号登录