Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible use multiple classes under the same namespace, in the same file

Tags:

Is it possible use multiple classes under the same namespace, in the same file? I want to do something like this:

<?php   namespace MyNamespace\Helpers\Exceptions    use Exception;    class CustomException1 extends Exception{}    class CustomException2 extends Exception{}    class CustomException3 extends Exception{} 

to avoid using one single file for each custom exception class. The problem is, when I try to use, in another class, one of the custom exceptions,

use MyNamespace\Helpers\Exceptions\CustomException1; 

the CustomException1 class is not found. Any ideas?

like image 656
Gligor Florin Avatar asked Mar 13 '15 13:03

Gligor Florin


People also ask

Can one namespace have multiple classes?

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.

Can we have two classes in the same file?

Yes, it can. However, there can only be one public class per . java file, as public classes must have the same name as the source file. One Java file can consist of multiple classes with the restriction that only one of them can be public.

Can we declare multiple namespaces in same file?

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.

Can you have multiple classes in a PHP file?

PHP doesn't support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes, we can implement it.


1 Answers

I don't think there's anything syntactically wrong with doing this, but I don't think any PSR-4 compliant auotloaders will be able to find a class that is not in it's own dedicated file since the standard is that the name of a file a class belongs in is the same as the name of the class itself:

  1. The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

Because of this, if you want to use this approach you will have to ensure to include that class file manually whenever you will need those classes to be defined (basically, anytime you want to throw / catch any of those exceptions).

An alternative is to define the classes you want to inside of another class' file that you are absolutely certain will always be autoloaded prior to any invocation of any new CustomExceptionN statements. You will probably find in the majority of cases it is a lot more trouble trying to remember to first be sure to autoload Class1 before using Class2 than it is to just follow the standard and include each class in it's own file located at the proper namespace path.

like image 176
Jeff Lambert Avatar answered Oct 12 '22 05:10

Jeff Lambert