Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php namespace all files in directory

Tags:

php

Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.

php manual on namespaces definition

Is it possible to give an entire directory a namespace without the necessity to declare the namespace at the beginning of each individual file?

like image 302
Adam Genshaft Avatar asked Apr 27 '26 18:04

Adam Genshaft


2 Answers

The direct answer is NO. You can declare multiple namespace for a single file but a single namespace cannot be declared to multiple file with a single line of statement. You have to do it in each files.

like image 155
Imran Avatar answered Apr 29 '26 10:04

Imran


It is possible for all your classes within a directory to use the same namespace, but you still have to declare the namespace inside each class file, for example if you had a directory structure:

/App/Core/

All classes in that folder could have the same namespace:

FileA.php

<?php
namespace App\Core;
class FileA {}

FileB.php

<?php
namespace App\Core;
class FileB {}

Depending on the code editor you use, you may be able to automate the process of updating all files so you don't have to.

like image 29
Rainner Avatar answered Apr 29 '26 10:04

Rainner