I’m trying to learn the namespaces feature in PHP, however how do I access classes that are in namespaces?
Like, say I have the class Users inside the namespace called Core, how do I access that namespace from the Pages namespace?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I believe this is what you’re after:
When you want to use a class that’s inside a namespace, you need to define the “absolute path” to the class, like I have done in the example. Note the
\before theCorenamespace, that tells PHP to use theCorenamespace that is located in the root or “global” namespace of PHP.So if you wanted to access the
Usersclass in yourPagesnamespace, you would do the following:There’s also another way to use the
Usersclass, which is:The
use \Core\Users;line allows you to use theUsersclass from theCorenamespace as if it were a normal class inside thePagesnamespace.