gpt4 book ai didi

php - 这个 php 语法有效吗? $类-> {'something' }()

转载 作者:可可西里 更新时间:2023-11-01 00:30:38 26 4
gpt4 key购买 nike

我在阅读某个代码示例时,注意到我不熟悉的语法。

$response = $controller->{'home'}();

这是一个有效的 php 语法吗?

最佳答案

是的。

$controller->{'home'}
// same as
$controller->home
// used to access a property

$controller->{'home'}()
// same as
$controller->home()
// used to call a method

主要好处是,通过调用 ->{stuff},您可以访问具有不同(或奇怪)名称的属性。

例子:

$a = new stdClass();
$a->{'@4'} = 4;

print_r($a);
// stdClass Object
// (
// [@4] => 4
// )

你不能做$a->@4,但可以做$a->{'@4'}

例如:https://3v4l.org/PaOF1

关于php - 这个 php 语法有效吗? $类-> {'something' }(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33586180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com