Lee
Lee

Reputation: 5936

PHP Namespace - Declare classes with the same name

Can a parent php script require two classes, each defined with the same class name.

I know by default this will cause an Error, so i have been looking into namespace. I do not want to hardcode the namespace into the class file as it would be inflexible and depend on the person writing the class, i would prefer to do the following.

// Define namespace
require_class('a.class.php');

// Define new namespace
require_class('b.class.php');

Thanks in advance

Upvotes: 3

Views: 5058

Answers (1)

Juicy Scripter
Juicy Scripter

Reputation: 25938

You are not allowed to define namespaces in such manner.

See Defining Namespaces:

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.

You can define multiple namespaces in the same file but the classes within that namespace should be place in this files too.

You may consider Using namespaces: Aliasing/Importing with allowing developers to specify initial namespace for classes.

Upvotes: 2

Related Questions