If I have some php classes inside a namespace com\test
and want to import all of them into another php file how can do that?
use com\test\ClassA
use com\test\ClassB
...
use com\test\*
give me syntax error.
Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name.
From PHP7 onwards, a single use statement can be used to import Classes, functions and constants from same namespace instead of multiple use statements.
But C# does not support multiple class inheritance. To overcome this problem we use interfaces to achieve multiple class inheritance.
Defining multiple namespaces in the same file ¶Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.
From PHP 7.0 onwards, classes, functions and constants being imported from the same namespace can be grouped together in a single use statement.
Like this way:
use com\test\{ClassA, ClassB};
$a = new ClassA;
$b = new ClassB;
This should work:
use com\test;
$a = new \test\ClassA;
$b = new \test\ClassB;
or
use com\test\ClassA as ClassA;
use com\test\ClassB as ClassB;
$a = new ClassA;
$b = new ClassB;
See the PHP manual on Using namespaces: Aliasing/Importing.
I think you can't do such thing.
You can do:
use com\test
and refer to your classes at later time as:
test\ClassA
test\ClassB
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