Php 什么是;返回$container->;{$resource}&引用;意思是

Php 什么是;返回$container->;{$resource}&引用;意思是,php,Php,brakets是什么意思?在哪里阅读更多 return $container->{$resource}; 两种可能性: $resource=“score”;//动态设置名称 返回$container->{$resource};//同返回$container->score 打字错误/初学者错误 程序员打算键入: return $container->resource; // returns resource public member variable 括号用于使用变量。这样更

brakets是什么意思?在哪里阅读更多

return $container->{$resource};
两种可能性:

  • $resource=“score”;//动态设置名称

    返回$container->{$resource};//同返回$container->score

  • 打字错误/初学者错误

  • 程序员打算键入:

    return $container->resource;  // returns resource public member variable
    

    括号用于使用变量。这样更容易区分:

    // gets the value of the "resource" member from the container object
    $container->resource;
    


    您可以在此处阅读更多内容:

    这不太可能是打字错误。很难“误打”一个美元符号和两个括号而不注意到。没错。{}很难输入错误,但是我看到了很多错误,程序员类型返回$container->$resource;请忽略Safraz的初始答案,因为PHP代码在单引号中永远不会被计算。但是,将对此进行评估:
    echo“{$container->$resource}”。它将作为对象
    $container
    的属性进行评估。属性名是
    $resource
    的值。然而,当使用双引号时,您的示例不会给出预期的结果。更多信息请参见Yada的答案:
    // gets the value of the "foo" member from the container object
    $resource = 'foo';
    $container->$resource;