Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP namespace for multiple files

I'm trying to include many files containing functions, classes and calls but I would like to put everything in a PHP namespace. My issue is : all the files are part of another web application core. So I cannot edit the files. Is there a way tu declare a namespace globally for a list of files ? Something like that :

namespace mynamespace {
    include_once("file1.inc.php");
    include_once("file2.inc.php");
    include_once("file3.inc.php");
    ...
}

Of course I tried this code and it does not work; all my tries failed while the first line of the include files where note namespace mynamespace;.

like image 784
Ludovic Avatar asked Nov 20 '25 22:11

Ludovic


1 Answers

No, it is not possible. Every single file needs to declare its own namespace; you cannot force something to be in a different namespace using external code. (Some hacks like runkit or such will probably allow you to fiddle with that, but I'd consider this outside the regular scope of PHP as a language.)

like image 70
deceze Avatar answered Nov 23 '25 11:11

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!