How to call PHP class from parent namespace?

I have the following namespace structure with the following class files

Application
|->App.php
|->Top
   |->Start.php
   |->Process.php
   |->Child
      |->Plugin.php

So in App.php I declared

namespace Application;

in the Startp.php ad

namespace Application\Top;

in the Plugin.php ad

namespace Application\Top\Child;

I see that I can call the Plugin.php class from App.php, for example

$object = new Top\Child\Plugin();

if it is 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 a double dot ".." to indicate the top parent directory in the namespace? I'm trying to name something like

File: Plugin.php

$object = new ..\Process();

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

$object = new \Application\Top\Process()

Is this the only option? Thanks!

+4
source share
1 answer

http://www.php.net/manual/en/language.namespaces.basics.php ( ) - root. ..\ . ,

use \Application\Top\Process as SomeProcess;

  $ object = new SomeProcess();

. $object = new..\Process(); :

$classPath = explode("\\", __NAMESPACE__);
array_pop($classPath);
$newClassPath = implode("\\", $classPath) . '\'. 'Process';
$object = new $newClassPath();
+5

All Articles