我理解字符串中复杂(卷曲)语法的用法,但我不理解它在字符串之外的用途.
我刚在CakePHP中找到了这个代码,我无法理解:
// $class is a string containg a class name
${$class} =& new $class($settings);
Run Code Online (Sandbox Code Playgroud)
如果有人能帮我理解为什么在这里使用,这和它有什么区别:
$class =& new $class($settings);
Run Code Online (Sandbox Code Playgroud)
谢谢.
理解这一点的最简单方法是通过示例:
class FooBar { }
// This is an ordinary string.
$nameOfClass = "FooBar";
// Make a variable called (in this case) "FooBar", which is the
// value of the variable $nameOfClass.
${$nameOfClass} = new $nameOfClass();
if(isset($FooBar))
echo "A variable called FooBar exists and its class name is " . get_class($FooBar);
else
echo "No variable called FooBar exists.";
Run Code Online (Sandbox Code Playgroud)
使用${$something}或$$something. 在 PHP 中称为“变量”。
所以在这种情况下,$FooBar创建了一个新变量叫,并且该变量$nameOfClass仍然只是一个字符串。