php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52504 Support relative namespaces
Submitted: 2010-07-31 09:58 UTC Modified: 2012-02-28 10:38 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: robert dot de dot wilde at online dot nl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.3 OS: Any
Private report: No CVE-ID: None
 [2010-07-31 09:58 UTC] robert dot de dot wilde at online dot nl
Description:
------------
It would be nice to have relative namespace support to keep code clean and flexible.

When inside of a namespace, it would be nice to have some directory-path-like option like '..'.

Test script:
---------------
namespace MyNs\Some\Path\Going\A\Long\Way
{
  class GoClass extends ..\..\Short\Way\GoClass  // <<
  {}
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-31 10:54 UTC] giorgio dot liscio at email dot it
very nice, i really like it

it would be nice too having * on import

works only if __autoload or spl_autoload_register is used, otherwise triggers an error

use MyNS\Test\*;       // imports all classes in the "Test" namespace
use MyNS\Test\**;      // imports all the namespace hierarchy (including subpackages) from namespace Test

__autoload($className, $importAll = FALSE, $importDeep = FALSE)
{
      // handle * as a full dir import
      // ** imports subdirs too
}

in my framework i need to put

use \FW\String;
use \FW\Int;
use \FW\Float;
use \FW\Vector;
use \FW\Dictionary;
use \FW\Types;

etc in every file...
 [2010-08-18 15:44 UTC] robert dot de dot wilde at online dot nl
Any developer can have a look?
 [2010-12-01 15:32 UTC] jani@php.net
-Package: *Programming Data Structures +Package: Class/Object related
 [2012-02-26 10:10 UTC] tom at tomwardrop dot com
I was just about to post the exact same feature suggestion. I'm using PHP 5.4 RC8 
after 2 years of programming Ruby (I have a project that better lends itself to 
PHP template-orientated nature), and this was one of the first things I tried to 
do, reference a resource one level up in the namespace hierarchy. Luckily, my 
namespace isn't too deep, but I can imagine some of the larger frameworks which 
have 3-6+ level deep namespaces could really benefit from this.

I'm surprised none of the dev's have commented on this.
 [2012-02-28 10:38 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2012-02-28 10:38 UTC] johannes@php.net
Adding support for the ..\..\Foo\Bar notation quickly requires quite some brain power to read the code, especially in a longer file where the namespace declaration might not be on the same screen. We try to keep PHP code human-readable.

Doing the use foo\*; thing is not possible due to PHP's dynamic nature. We do not know which classes there might be (think __autoload) so we'd like to do it but can't.
 [2012-04-02 21:01 UTC] francescrosasbosque at gmail dot com
As I understand it accessing parent namespaces would require the same brain power 
that it needs to access child namespaces as both of them rely on knowing what's 
the current namespace. To me it is weird to have one but not the other.
 [2013-03-19 12:01 UTC] vitaly dot mukhin at gmail dot com
Two cases:

namespace Language\Programming\Scripting;
{
  class Php extends ..\Scripting
  {}
}

VS 

namespace Language\Programming\Scripting;
{
  class Php extends \Language\Programming\Scripting
  {}
}

This example, I suppose, covers a huge percentage of class extendings. And, as you can see, first variant is much more easier and convenient in using.
 [2013-11-05 18:50 UTC] mtdowling at gmail dot com
This feature request would allow libraries to be written in a way that they can be vendored inside of other libraries without having to modify the library being vendored. Other languages, e.g., Python, allow this type of relative namespace traversal.

I don't agree that it would be hard to read or require "more brain power".
 [2014-10-05 17:16 UTC] dodonnell at gmail dot com
Agreed that in my opinion ..\ does not require more brainpower. I think the syntax is pretty self-explanatory to pretty much any one who knows some basic programming and, more importantly, prevents having to use a lot of absolute namespacing that is sensitive to structural changes in a project.
 [2014-11-11 09:34 UTC] andy at andyburton dot co dot uk
Agree this would be a nice addition.
 [2015-11-12 07:33 UTC] slegersjohn at gmail dot com
johannes@php.net said :
"Adding support for the ..\..\Foo\Bar notation quickly requires quite some brain power to read the code"

Seriously? I'm, it not only reads to MORE readable code, but it makes your code a hell of a lot more maintainable.
 [2018-05-03 19:36 UTC] robert at interjinn dot com
I'd really like to see parent relative path support also. It would be much more readable than all these absolute namespace paths or a use at the top for a single instance. We're all used to filesystem paths, reading ..\ takes minimal brainpower and I'd argue makes the code MORE readable.
 [2020-09-21 10:20 UTC] admin at manix dot info
10 years in, maybe it's time this gets some attention?
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Oct 01 14:05:52 2022 UTC