Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use an anonymous function inside a class in PHP?

How do I call $greet inside this class? I am using PHP 5.5.4.

<?PHP   
class Model
{
    public $greet = function($name)
    {
        printf("Hello %s\r\n", $name);
    };
}

$test = new Model();
$test->greet('World');
$test->greet('PHP');
?>

Parse error: syntax error, unexpected '$greet' (T_VARIABLE), expecting function (T_FUNCTION)

Also tried this,

$test = new Model();
call_user_func($test->greet('World'));
call_user_func($test->greet('PHP')) 

The anonymous function works fine outside the class (straight from the manual).

<?php
$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');
?>

EDIT: I took out the dollar signs, in my call (I caught it just as an answer started to roll in. It did not help,

call_user_func($test->greet('World'));
call_user_func($test->greet('PHP'));

EDIT:

class Model
{
    public $greet;
    function __construct()
    {
        $this->greet = function($name)
        {
            printf("Hello %s\r\n", $name);
        };
    }
}

$test = new Model();
$test->greet('johnny');

Now I get,

Fatal error: Call to undefined method Model::greet() 
like image 829
johnny Avatar asked Jun 15 '15 19:06

johnny


People also ask

What is the use of anonymous function in PHP?

Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.

How do you use anonymous function?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

What is an anonymous class in PHP?

As th name suggests, anonymous class is the one which doesn't have name. It is meant for one time use, and if one needs to define a class on the fly. Feature of anonymous class has been introduced since PHP 7 version. Definition of anonymous class lies inside an expression whose result is an object of that class.

Does PHP support anonymous classes?

In PHP, an anonymous class is a useful way to implement a class without defining it. These are available from PHP 5.3 onwards.


1 Answers

The fact that you call greet makes PHP treat it like a function and not a property. You can have the same name for both a property and method in PHP, so the distinction is relevant.

PHP 7+

In PHP7 the __call() method is no longer needed to call closures bound to properties, because of Uniform Variable Syntax. This will allow you to add parentheses around any code, just like you do in arithmetics.

class Model
{
    public $greet;
    function __construct()
    {
        $this->greet = function($name)
        {
            printf("Hello %s\r\n", $name);
        };
    }
}

$test = new Model();
($test->greet)('johnny');

PHP 5

What you can doe as a workaround is use the __call() magic method. It will catch the call to the undefined greet method.

class Model
{
    public $greet;
    function __construct()
    {
        $this->greet = function($name)
        {
            printf("Hello %s\r\n", $name);
        };
    }

    function __call($method, $args)
    {
        if (isset($this->$method) && $this->$method instanceof \Closure) {
            return call_user_func_array($this->$method, $args);
        }

        trigger_error("Call to undefined method " . get_called_class() . '::' . $method, E_USER_ERROR);
    }
}

$test = new Model();
$test->greet('johnny');
like image 190
Arnold Daniels Avatar answered Sep 27 '22 18:09

Arnold Daniels