如何使用复杂(卷曲)语法的常量?

发布于 2024-09-17 16:31:21 字数 337 浏览 8 评论 0原文

我很惊讶地发现以下内容没有按预期工作。

define('CONST_TEST','Some string');
echo "What is the value of {CONST_TEST} going to be?";

输出: {CONST_TEST} 的值是多少?

有没有办法解决花括号内的常量?

是的,我知道我可以这样做

echo "What is the value of ".CONST_TEST." going to be?";

,但我不想连接字符串,不是为了性能而是为了可读性。

I was surprised to see that the following doesn't work as expected.

define('CONST_TEST','Some string');
echo "What is the value of {CONST_TEST} going to be?";

outputs: What is the value of {CONST_TEST} going to be?

Is there a way to resolve constants within curly braces?

Yes, I am aware I could just do

echo "What is the value of ".CONST_TEST." going to be?";

but I'd prefer not to concatanate strings, not so much for performance but for readability.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

つ低調成傷 2024-09-24 16:31:21

不,这是不可能的,因为 php 会将 CONST_TEST 视为单/双引号内的一个简单的字符串。为此,您必须使用串联

echo "What is the value of ".CONST_TEST." going to be?";

Nope that's not possible because php will consider CONST_TEST to be a mere string inside the single/double quotes. You will have to use the concatenation for that.

echo "What is the value of ".CONST_TEST." going to be?";
慕烟庭风 2024-09-24 16:31:21

这可能不可能,但由于您的目标是可读性,因此您可以使用 sprintf/printf 来获得比字符串连接更好的可读性。

define('CONST_TEST','Some string');
printf("What is the value of %s going to be?", CONST_TEST);

It may not be possible, but since your goal is readability, you could use sprintf/printf to achieve better readability than through string concatenation.

define('CONST_TEST','Some string');
printf("What is the value of %s going to be?", CONST_TEST);
止于盛夏 2024-09-24 16:31:21

我不明白为什么你必须大惊小怪,但你总是可以这样做:

define('CONST_TEST','Some string');
$def=CONST_TEST;
echo "What is the value of $def going to be?";

i don't understand why you have to make a big fuss out of it but you can always do:

define('CONST_TEST','Some string');
$def=CONST_TEST;
echo "What is the value of $def going to be?";
错爱 2024-09-24 16:31:21

如果您非常想要该功能,您可以使用反射编写一些代码来查找所有常量及其值。然后将它们设置在像 $CONSTANTS['CONSTANT_NAME']... 这样的变量中,这意味着如果您想将常量放入字符串中,可以使用 {}。另外,不要将它们添加到 $CONSTANTS 中,而是使其成为一个实现 arrayaccess 的类,这样您就可以强制其中的值不能以任何方式更改(只有添加到对象中的新元素才能更改)作为数组访问)。

因此,使用它看起来像:

$CONSTANTS = new constant_collection();

//this bit would normally be automatically populate using reflection to find all the constants... but just for demo purposes, here is what would and wouldn't be allowed.
$CONSTANTS['PI'] = 3.14;
$CONSTANTS['PI'] = 4.34; //triggers an error
unset($CONSTANTS['PI']); //triggers an error
foreach ($CONSTANTS as $name=>$value) {
    .... only if the correct interface methods are implemented to allow this
}
print count($CONSTANTS); //only if the countable interface is implemented to allow this

print "PI is {$CONSTANTS['PI']}"; //works fine :D

要使您只需要输入一些额外的字符,您可以使用 $C 而不是 $CONSTANTS ;)

希望有所帮助,斯科特

If you wanted that feature really badly, you could write a little code using reflection that finds all the constants and their values. Then sets them inside a variable like $CONSTANTS['CONSTANT_NAME']... this would then mean if ever you want to put a constant in a string you can using {}. Also, rather than add them to $CONSTANTS, make it a class that implements arrayaccess so you can enforce that the values in it can not be changed in any way (only new elements added to the object which can be accessed as an array).

So using it would look like:

$CONSTANTS = new constant_collection();

//this bit would normally be automatically populate using reflection to find all the constants... but just for demo purposes, here is what would and wouldn't be allowed.
$CONSTANTS['PI'] = 3.14;
$CONSTANTS['PI'] = 4.34; //triggers an error
unset($CONSTANTS['PI']); //triggers an error
foreach ($CONSTANTS as $name=>$value) {
    .... only if the correct interface methods are implemented to allow this
}
print count($CONSTANTS); //only if the countable interface is implemented to allow this

print "PI is {$CONSTANTS['PI']}"; //works fine :D

To make it so you only have a few extra characters to type you could just use $C instead of $CONSTANTS ;)

Hope that helps, Scott

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文