2010-02-20 39 views
3

什麼的brakets意味着,在哪裏讀更多

return $container->{$resource}; 
+1

請忽略Safraz」最初的回答,因爲PHP代碼將永遠不會被單時評估引號。然而,這*會被評估:'echo「{$ container - > $ resource}」;'。它將被評估爲對象'$ container'的屬性。屬性名稱是'$ resource'的值。但是,您的示例在放入雙引號時不會給出預期的結果。請參閱雅達的答案更多信息:http://stackoverflow.com/questions/2302531/what-does-return-container-resource-mean/2302545#2302545 – 2010-02-20 15:00:25

回答

3

兩種可能性:

  1. variable variable

    $ resource =「score」; //動態設置名稱

    return $ container - > {$ resource}; //和return $ container-> score一樣;

  2. 錯字/初學者的錯誤

想鍵入程序員:

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

這不太可能是一個錯字。這將是很難「錯誤」美元符號和兩個括號,並沒有注意到。 – 2010-02-20 14:57:27

+1

是的。 {}很難輸入,但是我發現很多程序員類型返回$ container - > $ resource; – Yada 2010-02-20 15:00:21

4

的支架是利用可變的變量。它可以更容易區分之間:

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

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

你可以在這裏閱讀更多:http://php.net/manual/en/language.variables.variable.php