内部没有此功能的PHP匿名函数

问题描述 投票:2回答:1

如何在没有这个的类中声明一个函数?

<?php

class modul {

    var $i=1;
    var $j=1;

    public function init(){
        $test1 = function($vRecord){
            return 'zwrot';
        };
        echo "<pre>";
        print_r($test1);
        echo "</pre>";
    }

}

$modul = new modul();
$modul->init();

我打印时的结果:

Closure Object
(
    [this] => modul Object
        (
            [i] => 1
            [j] => 1
        )

    [parameter] => Array
        (
            [$vRecord] => 
        )

)

如何在不使用键的情况下传递功能:'this'?我想要一个在print_r中的纯函数,结果如下:

代码:

<?php
$test2 = function($vRecord){
    return 'zwrot';
};
echo "<pre>";
    print_r($test2);
echo "</pre>";

打印时的结果:

Closure Object
(
    [parameter] => Array
        (
            [$vRecord] => 
        )

)
php this anonymous-function
1个回答
1
投票

您可以定义static闭包:

    $test1 = static function($vRecord){
        return 'zwrot';
    };
© www.soinside.com 2019 - 2024. All rights reserved.