0

I'm using PHP 7.1.11 on my machine.

Consider below working code :

<?php
  class Foo {
    public $bar;

    public function __construct() {
      $this->bar = function() {
        return 42;
      };
    }
  }

  $obj = new Foo();

  // as of PHP 7.0.0:
  echo ($obj->bar)(), PHP_EOL;
?>

You can see that the anonymous function has been assigned to the class property bar but this has been done in a constructor.

Can I define the same anonymous function outside the constructor or any other method i.e. at the type of property declaration itself and can call it from within any class method using $this?

I tried below code but I got Fatal Error in output :

<?php
  class Foo {
    public $bar = function() {
      return 42;
    };

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

  $obj = new Foo();
  //as of PHP 7.0.0:
  echo $obj->bar, PHP_EOL;    
?>

Output :

Fatal error: Constant expression contains invalid operations

I want the same output as working code 42

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Obvious question: why not make it a regular `public function bar()`? – deceze Nov 27 '17 at 09:49
  • @deceze : That's the normal way around and I know it. I'm trying to learn the usage of anonymous classes inside the class, so experimenting with anonymous classes and trying to understand the concept thoroughly. –  Nov 27 '17 at 09:51
  • Fair enough. Then the practical answer is that there's little point in doing this and therefore there's no useful supported way to do it. – deceze Nov 27 '17 at 09:54
  • @deceze : Are you saying that anonymous functions in class should always be defined inside some class method or constructor and never be assigned to the class property directly outside of any class method or constructor? –  Nov 27 '17 at 09:57
  • It makes little sense to have a public class property be an anonymous function. If any `$o->p` is a function, you'd expect it to be callable as `$o->p()`, which doesn't work with anonymous functions and you have to call them as `($o->p)()`. That's largely nonsense, therefore that's not the place to use anonymous functions in the first place. – deceze Nov 27 '17 at 10:06
  • It's not purely a question of best practice - you can't initialise *any* class property to an anonymous function, public or otherwise. Class properties have to evaluate to a constant value at compile-time, which functions do not. See https://stackoverflow.com/questions/1633012/initialize-class-property-with-an-anonymous-function – iainn Nov 27 '17 at 10:10
  • @iainn : Ok. Is it mandatory to define anonymous function in a class constructor only? Can the anonymous function be defined inside any other class method than the constructor? –  Nov 27 '17 at 10:13
  • Yes, the important part is that it's defined *at runtime*, so the constructor, or another class method, or just setting the property itself will all work correctly. You just can't define it as the property's default value. – iainn Nov 27 '17 at 10:15
  • @iainn : Would you please combine all your points and put them in a well manner as an answer to this question? So, that it would be easy to refer also? Please put all your sayings as an answer. Thank You so much. Waiting keenly for answer from you. –  Nov 27 '17 at 10:17

0 Answers0