PHP 8.3.4 Released!

Das Schlüsselwort namespace und die magische Konstante __NAMESPACE__

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

PHP unterstützt zwei Arten des abstrakten Zugriffs auf Elemente innerhalb eines Namespaces, die magische Konstante __NAMESPACE__ und das Schlüsselwort namespace.

Der Wert von __NAMESPACE__ ist ein String, der den Namen des aktuellen Namespace beinhaltet. In globalem Code ohne Namespaces beinhaltet dies einen leeren String.

Beispiel #1 __NAMESPACE__-Beispiel, Code mit Namespace

<?php
namespace MyProject;

echo
'"', __NAMESPACE__, '"'; // gibt "MyProject" aus
?>

Beispiel #2 __NAMESPACE__-Beispiel, globaler Code

<?php

echo '"', __NAMESPACE__, '"'; // gibt "" aus
?>
Die Konstante __NAMESPACE__ ist nützlich, um dynamische Namen zu konstruieren, zum Beispiel:

Beispiel #3 __NAMESPACE__ zur dynamischen Namenszusammensetzung verwenden

<?php
namespace MyProject;

function
get($classname)
{
$a = __NAMESPACE__ . '\\' . $classname;
return new
$a;
}
?>

Das Schlüsselwort namespace kann verwendet werden, um explizit ein Element des aktuellen Namespaces oder eines untergeordneten Namespaces anzufordern. Es ist das äquivalent der Namespaces zum self-Operator für Klassen.

Beispiel #4 Der namespace-Operator, innerhalb eines Namespace

<?php
namespace MyProject;

use
blah\blah as mine; // siehe "Namespaces verwenden: Aliase/Importieren"

blah\mine(); // ruft die Funktion MyProject\blah\mine() auf
namespace\blah\mine(); // ruft die Funktion MyProject\blah\mine() auf

namespace\func(); // ruft die Function MyProject\func() auf
namespace\sub\func(); // ruft die Function MyProject\sub\func() auf
namespace\cname::method(); // ruft die statische Methode "method" der Klasse MyProject\cname auf
$a = new namespace\sub\cname(); // erzeugt ein Objekt der Klasse MyProject\sub\cname
$b = namespace\CONSTANT; // weist den Wert der Konstante MyProject\CONSTANT $b zu
?>

Beispiel #5 Der namespace-Operator, in globalem Code

<?php

namespace\func
(); // ruft die Function func() auf
namespace\sub\func(); // ruft die Function sub\func() auf
namespace\cname::method(); // ruft die statische Methode "method" der Klasse cname auf
$a = new namespace\sub\cname(); // erzeugt ein Objekt der Klasse sub\cname
$b = namespace\CONSTANT; // weist den Wert der Konstante CONSTANT $b zu
?>

add a note

User Contributed Notes 3 notes

up
88
a dot schaffhirt at sedna-soft dot de
14 years ago
Just in case you wonder what the practical use of the namespace keyword is...

It can explicitly refer to classes from the current namespace regardless of possibly "use"d classes with the same name from other namespaces. However, this does not apply for functions.

Example:

<?php
namespace foo;
class
Xyz {}
function
abc () {}
?>

<?php
namespace bar;
class
Xyz {}
function
abc () {}
?>

<?php
namespace bar;
use
foo\Xyz;
use
foo\abc;
new
Xyz(); // instantiates \foo\Xyz
new namespace\Xyz(); // instantiates \bar\Xyz
abc(); // invokes \bar\abc regardless of the second use statement
\foo\abc(); // it has to be invoked using the fully qualified name
?>

Hope, this can save someone from some trouble.

Best regards.
up
-1
bharatthapa45 at gmail dot com
1 year ago
Difference between __NAMESPACE__ and keyword 'namespace' that I find relevant is when invoking a class:

<?php
namespace MyApp;

class
App {
static function
app(){
echo
'hello app';
}
}

// this will work:
$obj = new namespace\App::app();

// this will not work
$obj = new __NAMESPACE__\App::app();

// however this will work:
$obj = __NAMESPACE__ . '\App';
$obj::foo();

?>
up
-26
cornichonche at gmail dot com
6 years ago
The example 4 is wrong.
Using php 7.2

<?php
namespace monProjet;

use function
blah\blah as mine;

blah\mine(); // Will NOT work
mine(); // Will work
?>
To Top