【问题标题】:php concatenating string to classconstant in array assignmentphp在数组赋值中将字符串连接到类常量
【发布时间】:2012-04-22 22:29:26
【问题描述】:

以下不起作用:

<?php
class test{
    const t = 10;
    public static $y = array('t' => self::t . 'hello');
}
var_dump(test::$y);
?>

谁能告诉我这是为什么? :)

它在 ... => self::t 中失败。 'hello')... 部分,作为接缝,它不能在 array() 中连接一个类常量。

这意味着以下两种情况都很完美:

public static $y = array('t' => self::t);

public static $y = array('t' => 'hello');

【问题讨论】:

  • 总是添加你的错误信息。如果你把它粘贴到搜索框中,你会得到 ca。 999 个相似的问题,即使不完全一样。

标签: php arrays class-constants


【解决方案1】:

谁能告诉我这是为什么?

串联是运行时的产物。解析时必须知道类成员初始值。

【讨论】:

    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2020-12-06
    • 2021-10-05
    相关资源
    最近更新 更多