# Desired namespace behavoir

- Thread: https://externals.io/message/20961
- Original archive: https://news-web.php.net/php.internals/20961
- Started by Bart de Boer on 2005-12-01T16:42:22+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #20961 — Bart de Boer — 2005-12-01T16:42:22+00:00

URL: https://externals.io/message/20961#20961

> /*
>  * this encounters a namespace. The auto-prefix-import option
>  * is active so EVERYTHING is inside the namespace is imported
>  * with a prefix of "JessieStuff_".
>  */
> require 'JessiePackage.php';
> $x=new JessieStuff_SimpleClass();


So let's say we've got a package like this:


namespace JessieStuff {

  class FirstClass { ... }

  class SecondClass {

    public $obj;

    function __construct() {
      $this->obj = new FirstClass();
    }
  }
}


Now we'd import this with prefixing everything and the
result would be something that acts like:


class JessieStuff_FirstClass { ... }

class JessieStuff_SecondClass {

  public $obj;

  function __construct() {
    $this->obj = new FirstClass();
  }
}


Then, if I do:

$x = new JessieStuff_SecondClass();

I'd get an error that 'FirstClass' can't be found. Because it's
renamed to JessieStuff_FirstClass.

Or do you suggest we replace all occurences of 'new ClassName()' to
'new JessieStuff_ClassName()' as well?

Or do you suggest that everything that was inside the namespace is
still able to call "internal" classes without using the namespace
prefix? And your INI option only provides interface classes?

---

## #20968 — oliver.graetz@arcor.de — 2005-12-01T17:07:47+00:00

URL: https://externals.io/message/20961#20968 · In reply to: #20961

Bart de Boer schrieb:
> namespace JessieStuff {
>   class FirstClass { ... }
>   class SecondClass {
>     public $obj;
>     function __construct() {
>       $this->obj = new FirstClass();
>     }
>   }
> }
> 
> Now we'd import this with prefixing everything and the
> result would be something that acts like:
> 
> class JessieStuff_FirstClass { ... }
> class JessieStuff_SecondClass {
>   public $obj;
>   function __construct() {
>     $this->obj = new FirstClass();
>   }
> }
> 
> Then, if I do:
> $x = new JessieStuff_SecondClass();
> 
> I'd get an error that 'FirstClass' can't be found. Because it's
> renamed to JessieStuff_FirstClass.

Nope. First of all, using FirstClass INSIDE the namespace would always
prefer using the own namespace. Local definitions override global
definitions (see http://en.wikipedia.org/wiki/XML_namespace ).
And then, the class is hopefully NOT renamed but aliased:

namespace a
{
  class A1 {}
  class A2
  {
    protected $m=null;
    protected $n=null;
    public function __construct()
    {
      // should both work
      $this->m=new A1();
      $this->m=new a:::A1();
    }
  }
}
$x=new a:::A2();

import namespace a;
$y=new A2(); // should work after import

import a:::A2 as Foo;
$z=new Foo(); // should work, A2 locally knows A1

$yikes=new a_A2(); // should work with auto_import_prefix set to on

> Or do you suggest we replace all occurences of 'new ClassName()' to
> 'new JessieStuff_ClassName()' as well?
Occurences where?

> Or do you suggest that everything that was inside the namespace is
> still able to call "internal" classes without using the namespace
> prefix? And your INI option only provides interface classes?
This is part of the definition of namespaces.
And yes, I want the INI option to just provide aliases.
If these are implemented by injecting

  class a_A2 extends a::A2 {}

as interface classes or by internally providing a hash table alias is
only different from a technical point of view. The result would be the
same, the performance would probably better using the alias.

OLLi

---

## #20980 — Bart de Boer — 2005-12-01T21:07:42+00:00

URL: https://externals.io/message/20961#20980 · In reply to: #20968

> This is part of the definition of namespaces.
> And yes, I want the INI option to just provide aliases.
> If these are implemented by injecting
> 
>   class a_A2 extends a::A2 {}
> 
> as interface classes or by internally providing a hash table alias is
> only different from a technical point of view. The result would be the
> same, the performance would probably better using the alias.
> 
> OLLi

Ah, my bad. I thought you proposed to rename the classes.
