3

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, i've read the manual thats why i am asking the question. I have a Modules/Controller.php that requires 2 files that could have the same class name from different modules, so im a little stuck on the solution..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.