What does $ a = new $ b () mean in PHP?

Although I understand that

$a = new b() 

will initialize the object for class b, but that would

 $a = new $b() 

means that I came across some kind of code that works differently!

+6
source share
2 answers

This is a reflexive reference to a 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 only for support: you can also call functions:

 function test(){ echo 123; } $a = "test"; $a(); //123 printed 
+6
source

This code:

 $b = "Foo"; $a = new $b(); 

equivalent to the following:

 $a = new Foo(); 

This means that you can use syntax like $b() to dynamically access a function name or class name.

+1
source

Source: https://habr.com/ru/post/895335/


All Articles