PHP类中的函数字面量 [英] Function literal in PHP class

查看:91
本文介绍了PHP类中的函数字面量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看这段代码,请:

  $ array = array(
'action'=> ; function(){echothis works;}
);
$ b $ class Test {
public $ array = array(
action=> function(){echothis does not;}
);
}

第一个函数文字解析得很好,但第二个 - - 触发语法错误:
$ b


解析错误:语法错误,意外的'function'(T_FUNCTION)...


有人可以向我解释这个吗?这是一个错误?



编辑:这是最新的PHP:5.6.6

解决方案<

$ b $

b

声明可能包含一个初始化,但是这个
初始化必须是一个常量值 - 也就是说,它必须能够
评估 strong>在编译时,并且必须不依赖于运行时
信息才能被评估。


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


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

解决方案

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

这篇关于PHP类中的函数字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆