PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Deklaration von Namensräumen> <Objects and references
Last updated: Fri, 14 Nov 2008

view this page in

Namensräume

Inhaltsverzeichnis

Überblick über Namensräume

PHP-Namensräume wurden entworfen, um das Problem der Sichtbarkeit in großen PHP-Bibliotheken zu lösen. In PHP sind alle Klassendefinitionen zunächst einmal global. Der Autor einer Bibliothek muss sich daher beim Anlegen von Hilfsklassen oder Bibliotheksklassen, die Teil des öffentlichen API der Klasse sind, bewusst sein, dass andere Bibliotheken mit ähnlicher Funktionalität existieren können, und muss daher eindeutige Namen wählen, so dass beide Bibliotheken koexistieren können. Dies wird typischerweise gelöst, indem jedem Klassennamen ein eindeutiger Vorspann vorangestellt wird. Datenbankklassen würden so z.B. mit Meine_DB_Bibliothek beginnen usw. Wenn die Bibliothek wächst, addieren sich die verschiedenen Vorspanntexte auf und können so zu sehr langen Namen führen.

Namensräume erlauben dem Entwickler, die Sichtbarkeit von Namen zu beeinflussen, ohne bei jedem Einsatz einer Klasse auf den langen Namen zurückzugreifen, und lösen so das Problem des gemeinsamen globalen Namensraums, ohne den Programmtext dabei unlesbar zu machen.

Namensräume sind in PHP ab PHP 5.3.0 verfügbar. Die Umsetzung ist noch experimentell und daher kann sich diese Dokumentation noch ändern.



Deklaration von Namensräumen> <Objects and references
Last updated: Fri, 14 Nov 2008
 
add a note add a note User Contributed Notes
Namensräume
greg at chiaraquartet dot net
21-Sep-2008 08:32
the previously submitted note is incorrect.

A::foo();
is identical to
::A::foo();

in the global namespace.

The only way to successfully call the static method is to instantiate A and call it like:

<?php
$a
= new A;
$a->foo();
?>

or to use reflection.  This limitation is addressed in patches I have proposed, so the implementation may yet be usable in this regard.
nickf
31-Aug-2008 06:01
In response to Amir Abiri's comment:

In this example:
<?php
//---
// global.php
class A {
  public static function
foo() {
    echo
"static function";
  }
}

//----
// namespace.php
namespace A;
function
foo() {
  echo
"namespace function";
}
?>
The way to call each of these functions is like so:
<?php
A
::foo();  // "namespace function"
::A::foo(); // "static function"
?>
This could get pretty confusing, but at least now you know. :)
j dot s dot lubbers at gmail dot com
01-Aug-2008 03:02
This is my 'ultimate' autoload functie:
<?php
   
function __autoload($class) {
        require(
'../includes/classes/' . str_replace('::', '/', strtolower($class)) . '.php');
    }

   
//When you do:
   
$object = new PDF::Document();
?>

it will include the file:
../includes/classes/pdf/document.php

as you will notice I like to keep my includes outside the webroot.

document.php
<?php
    namespace PDF
;

    class
Document {
       
//etc...
   
}
?>
Tito
16-Jul-2008 09:31
you can use __autoload to automaticly include Classes with a "use" / "new" statement.
yarco dot w at gmail dot com
01-Jul-2008 01:19
So do you mean if i want to use a class, i need to do extra two steps?

1) require/include that file
2) use the namespace

What about to add a trigger something like:

function __auto_namespace($names, $class)
{
  if ($class === null)
  {
    set_include_dir(implode('/', $names));
  }
  else
  {
    require_once implode('/', $names).'/'.$class.'.php';
  }
}

Then when we:
use NAMESPACE1::NAMESPACE2;
or
use NAMESPACE1::NAMESPACE2::CLASS1;

php could auto include the file we needed.
Amir Abiri
25-Dec-2007 05:31
So, if I understand correctly there is a possible ambiguity that can cause a function or method to become "masked".

If I have:

global.php:
<?php
class A
{
    static public function
foo()
    {
    }
}

A::foo(); // Will statically call method foo() of class ::A.
?>

If I now added the following to my project:

A.php:
<?php
namespace A
;

function
foo()
{
}
?>

The function call above would instead call this new function.

It shouldn't be a problem most of the time and specially if certain basic practices are followed (For example, don't name classes and namespaces the same name, and always keep different packages in their own separate namespaces), but it's something to keep in mind.

Deklaration von Namensräumen> <Objects and references
Last updated: Fri, 14 Nov 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites