0

I have a question about namespaces in PHP.

this code doesn't work :

<?php
namespace My\Functions\Printing;

class A {
    public function __construct() {
        echo __NAMESPACE__;
    }
}


namespace My;
use My\Functions\Printing\A as A;


$obj=new namespace\A();

But this one work :

<?php
namespace My\Functions\Printing;

class A {
    public function __construct() {
        echo __NAMESPACE__;
    }
}


namespace My;
use My\Functions\Printing\A as A;


$obj=new A();

I would like to get more information about the behavior of namespaces importation. Why an imported class can't be accessible in the namespace where it is imported?

4
  • I do not understand... In the first example you try to run My\A, and obviously does not work. The second example is the correct way to proceed. What is your question? Commented Jul 13, 2012 at 9:58
  • It works like the example that works. just view them as directories. If you are IN a directory (namespace) you can access files from that path, or use the actual complete path. If you add a shortcut ("use") than you can access that link directly trough the name. Commented Jul 13, 2012 at 10:03
  • Laxus : the use command import class A in My namespace so i don't understand why use namespace\A (\My\A) doesn't work. Commented Jul 13, 2012 at 10:12
  • Nanne : Ok but after use the use commande (created a link in directory) why i can't access the class by the complete path (\My\A) Commented Jul 13, 2012 at 10:15

3 Answers 3

1

Probably you have a confused idea of using 'use'. The keyword 'namespace' refers to the current namespace

namespace My\Functions\Printing;

class A {
    public function __construct() {
        echo __NAMESPACE__;
    }
}

namespace My;
use My\Functions\Printing\A;
use My\Functions\Printing\A as myAlias;

$obj=new namespace\A(); // instance of  \My\A (doesn't exist)
$obj2=new A(); // instance of  \My\Functions\Printing\A
$obj3=new myAlias(); // instance of  \My\Functions\Printing\A

As usual, see the documentation for complete details: php doc

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure but I think problem is in using namespace keyword.

http://www.php.net/manual/pl/language.namespaces.nsconstants.php

In first example you are in My namespace so namespace\A() == My\My\Functions\Printing\A()

3 Comments

\My\A doesn't exist! You need to use \My\Functions\Printing\A Read: php.net/manual/en/language.namespaces.php
I guest A is resolved to My\Functions\Printing\A 'couse You are using alias "use My\Functions\Printing\A as A;" So new \My\A still is resolved \My\My\Functions\Printing\A.
No because \My\A doesn't exist too.
0

My\Functions\Printing namespace look like

-- My\Functions\Printing

  • A

"My" namespace before importation look like:

-- My

"My" namespace after importation should look like (i thinks)

-- My

  • A

So why i can't acces My\A ?

I think the structure of namespace it's not changed an PHP compiler check in the "namespace imported area" before the "namespace structure".

Documentation specify namespaces look like filesystem, it's false.

If an importation of an other class is as "create a symbolic link" i must be able to acced it from the current namespace. We can acced a symbolic link from the directory where it was created or from the absolute path but it's not possible to acced the imported class with its new path. (\My\A)

Why ?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.