Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function literal in PHP class

Take a look at this code, please:

$array = array(
    'action' => function () { echo "this works"; }
);

class Test {
    public $array = array(
        "action" => function () { echo "this doesn't"; }
    );
}

The first function literal parses fine, but the second - the one inside the class - triggers a syntax error:

Parse error: syntax error, unexpected 'function' (T_FUNCTION)...

Can somebody explain this to me? Is this a bug?

EDIT: This is the latest PHP: 5.6.6


1 Answers

From the class it's a property !

Rule from properties :

Declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

http://php.net/manual/en/language.oop5.properties.php

like image 63
Bang Avatar answered Jan 30 '26 18:01

Bang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!