Although I get it that
虽然我知道。
$a = new b()
would be initializing an object for the class b, but what would
将初始化类b的对象,但是什么呢
$a = new $b()
mean because I came across some code that happens to work otherwise!
意思是,因为我遇到了一些碰巧有用的代码!
2 个解决方案
#1
6
It's a reflexive reference to the class with a name that matches the value of $b
.
它是对具有与$b值匹配的名称的类的反射引用。
Example:
例子:
$foo = "Bar";
class Bar
{
...code...
}
$baz = new $foo();
//$baz is a new Bar
Update just to support: you can call functions this way too:
更新只是为了支持:您也可以这样调用函数:
function test(){
echo 123;
}
$a = "test";
$a(); //123 printed
#2
1
This code:
这段代码:
$b = "Foo";
$a = new $b();
is equivalent to the following:
等于:
$a = new Foo();
Meaning that you can use syntax like $b()
to dynamically refer to a function name or a class name.
这意味着您可以使用$b()之类的语法来动态引用函数名或类名。
#1
6
It's a reflexive reference to the class with a name that matches the value of $b
.
它是对具有与$b值匹配的名称的类的反射引用。
Example:
例子:
$foo = "Bar";
class Bar
{
...code...
}
$baz = new $foo();
//$baz is a new Bar
Update just to support: you can call functions this way too:
更新只是为了支持:您也可以这样调用函数:
function test(){
echo 123;
}
$a = "test";
$a(); //123 printed
#2
1
This code:
这段代码:
$b = "Foo";
$a = new $b();
is equivalent to the following:
等于:
$a = new Foo();
Meaning that you can use syntax like $b()
to dynamically refer to a function name or a class name.
这意味着您可以使用$b()之类的语法来动态引用函数名或类名。