Sławomir Kudła
Sławomir Kudła

Reputation: 291

PHP anonymous function without this inside

how to declare a function inside a class without this?

<?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();

my result when i print:

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

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

)

how to pass function without key: 'this'? i want a pure function in print_r and resoult like this:

code:

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

result when i print:

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

)

Upvotes: 4

Views: 83

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

You can define a Static anonymous function:

    $test1 = static function($vRecord){
        return 'zwrot';
    };

Upvotes: 6

Related Questions