Although I get it that
$a = new b()
would be initializing an object for the class b, but what would
$a = new $b()
mean because I came across some code that happens to work otherwise!
It's a reflexive reference to the class with a name that matches the value of $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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With