6

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() 
Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
johnny
  • 19,272
  • 52
  • 157
  • 259

2 Answers2

8

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');
Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82
  • Why do you have to assign the function in a constructor or some other method? It won't work if you don't (for me). – johnny Jun 15 '15 at 21:17
  • You mean the `Parse error` from your first example? Using the `function` keyword in the class definition means creating a method, not an anonymous function. – Arnold Daniels Jun 15 '15 at 22:05
  • No, I mean when you assign the anonymous function to the variable. It has to be done in a function (or the constructor). It can't simply be a member variable. – johnny Jun 16 '15 at 13:07
  • 1
    @johnny It's a 2 stage thing. The PHP code is first interpreted (by the [Zend engine](https://en.wikipedia.org/wiki/Zend_Engine)) and than executed. The class definition (with methods and properties) is handled by the interpreter, while creating objects, anonymous functions, etc is done when the code is executed. – Arnold Daniels Jun 17 '15 at 14:44
  • 1
    @johnny In PHP7 the `__call` method will no longer be needed. PHP7 is in alpha right now. I've updated the answer accordingly. – Arnold Daniels Jun 18 '15 at 05:02
3

You cannot initialize object variables with the results of expressions. Only static/constant values are allowed. e.g.

class foo {
   public $bar = 1+1; // illegal - cannot use an expression
   public $baz = 2; // valid. '2' is a constant
}

You'd have to do:

class foo {
   public $bar;
   function __construct() {
        $this->bar = function() { .... }
   }
}

And to actually invoke the closure, as per this answer:

$x = new foo()
$x->bar->__invoke();
Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • How do I call it outside the Class? Do I have to wrap it in another function that returns the value like any old variable usually does? I tried various ways but they do not work. – johnny Jun 15 '15 at 20:02
  • if you build the closure in the constructor, `$bar` will be "pointing" at the closure by the time the `new` call returns. so `$x = new foo(); $x->bar();` should work as expected. – Marc B Jun 15 '15 at 20:04
  • Can you please tell me what I am doing wrong in my Edit above? Edit: Sorry. I forgot to put in the edit! – johnny Jun 15 '15 at 20:12
  • Thanks. That seems like a lot of trouble for this, so maybe I need to rethink using it. It looks a lot different in Javascript, for example. – johnny Jun 15 '15 at 20:17
  • 1
    yeah, well, .... welcome to php. where consistency doesn't matter and everything's made up. – Marc B Jun 15 '15 at 20:18
  • If you can edit your answer to put in that link, I can mark it answered. I appreciate all the help. – johnny Jun 15 '15 at 20:20