Skip to content
Advertisement

How to call PHP a class from parent namespace?

I have the following namespace structure with the following class files

JavaScript

So in App.php I’ve declared

JavaScript

in Startp.php declared

JavaScript

in Plugin.php declared

JavaScript

I see that I can call the Plugin.php class from the App.php like

JavaScript

if it’s a child/grandchild namespace, but what if I want to call Process.php from Plugin.php, which is the parent namespace from the branch? Is there something like double dots “..” to indicate the upper parent directory in namespace? I am trying to call something like

JavaScript

it didn’t work, it looks like I can only start all the way from the root like

JavaScript

Is this the only option? Thank you!

Advertisement

Answer

As it mentioned here http://www.php.net/manual/en/language.namespaces.basics.php (in comments) there is only one way to specify namespaces – from root. You can not use .. to shorten namespace calls. To shorten calls in you code you can use

JavaScript

and use, first at your code

JavaScript

Also you can write your class loader. To call $object = new ..Process(); Here is the sketch:

JavaScript

Be sure to use double backslash when you want to use them in single quotes.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement