PHP Doku:: Namespaces definieren - language.namespaces.definition.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzNamespacesNamespaces definieren

Ein Service von Reinhard Neidl - Webprogrammierung.


<<Übersicht über Namespaces

Unter-Namespaces deklarieren>>

Namespaces definieren

Obwohl jeder gültige PHP-Quellcode in Namespaces eingeschlossen werden kann, werden nur drei Arten von Code von Namespaces beeinflusst: Klassen, Funktionen und Konstanten.

Namespaces werden mit dem Schlüsselwort namespace definiert. Eine Datei, die einen Namespace beinhaltet, muss den Namespace am Anfang der Datei vor jeglichem anderen Code deklarieren - mit Ausnahme des declare-Schlüsselworts.

Beispiel #1 Einen einzelnen Namespace deklarieren

namespace MyProject;

Connection /* ... */ }
connect() { /* ... */  }

Der einzige Quellcode, der vor einer Namespacedeklaration stehen darf, ist der declare-Ausdruck, der die Kodierung der Quellcodedatei definiert. Gleichzeitig darf kein Nicht-PHP-Code vor einer Namespacedeklaration stehen - auch keine zusätzlichen Leerzeichen, Zeilenumbrüche oder Tabulatoren.

Beispiel #2 Einen einzelnen Namespace deklarieren

namespace MyProject// fatal error - Namespace muss der erste Ausdruck im Skript sein

Zusätzlich darf - im Gegensatz zu anderen PHP-Konstrukten - der selbe Namespace in mehreren Dateien definiert werden, womit man den Inhalt eines Namespaces im Dateisystem aufteilen kann.

7 BenutzerBeiträge:
- Beiträge aktualisieren...
huskyr at gmail dot com
5.10.2009 13:20
"A file containing a namespace must declare the namespace at the top of the file before any other code"

It might be obvious, but this means that you *can* include comments and white spaces before the namespace keyword.

// Lots
// of
// interesting
// comments and white space

namespace Foo;
Bar {
jurrien at jpdokter dot nl
4.08.2009 22:21
I have written a Packager class that allows to use packages and namespaces. It does require the folder layout. If the __autoload function is not yet "re-implemented", it'll create its own __autoload function.

It is written in PHP5 and does not require > PHP5.3, moreover, I don't have PHP5.3 since XAMPP is not adding it yet. Don't ask me why.

Since it's over 300 lines long, I am not going to post it here. But if someone would like to have a look?

Basic uses:


# /classes
#     /package1
#         class34.class.php
#     /package2
#         classone.class.php



Packager::addClasspath(dirname(__FILE__).'/classes', true); // true means that it overrules the predefined set.
Packager::addClassSuffix(array('.class.php'),true); // true means that it overrules the predefined set.

Packager::import("package1.Class34"); // java-style
Packager::import("package2::ClassOne"); // php-style

echo Packager::dump(); // show us what you got!

jeremeamia at gmail dot com
14.07.2009 17:43
You should not try to create namespaces that use PHP keywords. These will cause parse errors.


namespace Project/Classes/Function; // Causes parse errors
namespace Project/Abstract/Factory; // Causes parse errors
danbettles at yahoo dot co dot uk
14.04.2009 21:02
Regarding constants defined with define() inside namespaces...

define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

namespace test;
define('MESSAGE', 'Hello world!');

The following code will define two constants in the "test" namespace.

namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
David Drakard
7.09.2008 14:56
I agree with SR, the new namespaces feature has solved a number of problems for me which would have required horrible coding to solve otherwise.

An example use:
Say you are making a small script, and write a class to connect to a database, calling it 'connection'. If you find your script useful and gradually expand it into a large application, you may want to rename the class. Without namespaces, you have to change the name and every reference to it (say in inheriting objects), possibly creating a load of bugs. With namespaces you can drop the related classes into a namespace with one line of code, and less chance of errors.

This is by no means one of the biggest problems namespaces solve; I would suggest reading about their advantages before citicising them. They provide an elegant solutions to several problems involved in creating complex systems.
14.05.2008 21:47
There is nothing wrong with PHP namespaces, except that those 2 instructions give a false impression of package management.
... while they just correspond to the "with()" instruction of Javascript.

By contrast, a package is a namespace for its members, but it offers more (like deployment facilities), and a compiler knows exactly what classes are in a package, and where to find them.
1.04.2008 21:11
@ RS: Also, you can specify how your __autoload() function looks for the files. That way another users namespace classes cannot overwrite yours unless they replace your file specifically.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)