串联__DIR__常量字符串作为数组值是在PHP类成员 [英] Concatenating __DIR__ constant with a string as an array value which is a class member in PHP

查看:103
本文介绍了串联__DIR__常量字符串作为数组值是在PHP类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,为什么不起作用?
它是什么,我试图做别的地方只是一个粗糙的例子。

  $ =东西阵列(
    钥匙= GT; __DIR__。 '值'
);

不过,这会产生一个错误:

  PHP解析错误:语法错误,意外在第6行/var/www/.../testing.php,预计')'。

此外,这个作品:

  $ =东西阵列(
    钥匙= GT; {} __DIR__值
);


解决方案

code的第一块不工作,因为它不是一个恒定的前pression,当你试图连接两个字符串。最初的类成员必须是恒定的。

文档


  

[属性]初始化必须是一个恒定值 - 也就是说,它必须能够在编译时进行评估,并以不能依赖于运行时的信息进行评估。


Can anyone tell me why this doesn't work? It's just a crude example of what I'm trying to do somewhere else.

$stuff = array(
    'key' => __DIR__ . 'value'
);

However, this produces an error:

PHP Parse error:  syntax error, unexpected '.', expecting ')' in /var/www/.../testing.php on line 6

Also, this works:

$stuff = array(
    'key' => "{__DIR__} value"
);

解决方案

The first piece of code does not work because it's not a constant expression, as you are trying to concatenate two strings. Initial class members must be constant.

From the documentation:

[property] 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.

这篇关于串联__DIR__常量字符串作为数组值是在PHP类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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