Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call PHP a class from parent namespace?

Tags:

namespaces

php

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've declared

namespace Application;

in Startp.php declared

namespace Application\Top;

in Plugin.php declared

namespace Application\Top\Child;

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

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

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

File: Plugin.php

$object = new ..\Process();

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

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

Is this the only option? Thank you!

like image 823
user702300 Avatar asked Dec 13 '13 04:12

user702300


People also ask

HOW include class from another file in PHP?

Four functions enable you to include code from another file: include(), require(), include_once(), and require_once(). All four can take a local file or URL as input.

What is the best approach for working with classes and namespaces in PHP?

To address this problem, namespaces were introduced in PHP as of PHP 5.3. The best way to understand namespaces is by analogy to the directory structure concept in a filesystem. The directory which is used to group related files serves the purpose of a namespace.

How does PHP namespace work?

Like C++, PHP Namespaces are the way of encapsulating items so that same names can be reused without name conflicts. It can be seen as an abstract concept in many places. It allows redeclaring the same functions/classes/interfaces/constant functions in the separate namespace without getting the fatal error.

Can I use two namespace in PHP?

Defining multiple namespaces in the same file ¶Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.


1 Answers

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

use \Application\Top\Process as SomeProcess;

and use, first at your code

$object = new SomeProcess();

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

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

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

like image 195
klipach Avatar answered Sep 21 '22 05:09

klipach