23

Example1:

namespace Inori\Test;

class MainTest { }

Example2:

namespace Inori\Test\SubTest;

class SubTest extends ???? { }

Question: is there a way to quickly go up a level in namespace so SubTest could extend MainTest? something like "../MainTest"? Or am I stuck with \Inori\Test\MainTest?

2
  • 4
    Namespaces in PHP cannot be nested: there is no hierarchy per say. Each namespace is declared independently from each other, there is no parent-child relationships between namespaces (yet). Foo\Bar is a totally different namespace than Foo\Bar\Baz. Commented Aug 30, 2011 at 15:50
  • So I came across this and realized that I didn't exactly need the parent namespace, I could do what I needed from the global namespace but didn't know how to get there. It turns out all you need to do is start the namespace with a backward slash and it won't use nesting namespacing. So if you're in the Foo\Bar namespace and call new Baz\Bat() it will be in the Foo\Bar\Baz\Bat() namespace. However if you call new \Baz\Bat() it will be in the Baz\Bat() namespace. Commented May 1, 2014 at 20:13

2 Answers 2

22

Relative namespaces aren't supported. There's a request for it though: https://bugs.php.net/bug.php?id=52504

If you import your classes at the top of the file it shouldn't be that big of a deal.

namespace Inori\Test\SubTest;
use Inori\Test\MainTest;

class SubTest extends MainTest { }
Sign up to request clarification or add additional context in comments.

1 Comment

That use is called aliasing/importing, which can be used in 5 ways: php.net/manual/en/language.namespaces.importing.php
1

See that accepted answer is already provided. However, hereby a code you can use to take advantage of relative namespaces (note: feel free to use code below for free and reference to author in your code is not required, no guarantees are provided by author and use of code is on own risk).

update: code can be used inside your class to dynamically and quickly load other classes via relative namespaces. Starter of this topic is looking for a way to extend class to other class via relative namespace, that remains not possible also not with this code.

In your class just add the following code:

public function TestRelativeNamespace()
{
    // (e.g., param1 = site\lib\firm\package\foo, param2 = .\..\..\different)
    $namespace = $this->GetFullNamespace(__NAMESPACE__, '.\\..\\..\\different');

    // will print namespace: site\lib\firm\different
    print $namespace;

    // to create object
    $className = $namespace . '\\TestClass';
    $test = new $className();
}

public function GetFullNamespace($currentNamespace, $relativeNamespace)
{
    // correct relative namespace
    $relativeNamespace = preg_replace('#/#Di', '\\', $relativeNamespace);

    // create namespace parts
    $namespaceParts = explode('\\', $currentNamespace);

    // create parts for relative namespace
    $relativeParts = explode('\\', $relativeNamespace);

    $part;
    for($i=0; $i<count($relativeParts); $i++) {
        $part = $relativeParts[$i];

        // check if just a dot
        if($part == '.') {

            // just a dot - do nothing
            continue;
        }
        // check if two dots
        elseif($part == '..') {

            // two dots - remove namespace part at end of the list
            if(count($namespaceParts) > 0) {

                // remove part at end of list
                unset($namespaceParts[count($namespaceParts)-1]);

                // update array-indexes
                $namespaceParts = array_values($namespaceParts);
            }
        }
        else {

            // add namespace part
            $namespaceParts[] = $part;
        }
    }

    if(count($namespaceParts) > 0) {
        return implode('\\', $namespaceParts);
    }
    else {
        return '';
    }

}

1 Comment

This really does not help with the original question about what to write in place of question marks in class child extends ???? { ... } where child inherits from namespace one level above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.