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.
Deklaration von Namensräumen
Ein Namensraum wird mit Hilfe des Schlüsselwortes namespace deklariert, welches direkt an den Dateianfang gesetzt werden sollte.
Beispiel #1 Beispiel zur Deklaration von Namensräumen
<?php
namespace MyProject::DB;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
?>
Namensräume können Definitionen von Klassen, Konstanten und Funktionen enthalten, aber keinen freien Programmtext.
Eine Namensraumdefinition bewirkt folgendes:
- Innerhalb eines Namensraumes wird allen Klassen, Funktionen und Konstanten automatisch der Name des Namensraumes vorangestellt. Der Klassenname ist immer der vollständige Name, d.h. in obigem Beispiel ist der eigentliche Klassenname MyProject::DB::Connection.
- Eine Konstantendefinition erzeugt eine Konstante deren vollstämdiger Name aus den Namen des Namensraum und der Konstantendefinition zusammengesetzt ist. Konstanten in Namensräumen können wie Klassenkonstanten nur statische Werte erhalten.
-
Ein nicht qualifizierter Klassenname (d.h. ein Name der nicht :: enthält) wird zur Laufzeit folgendermaßen aufgelöst:
- Die Klasse wird im aktuellen Namensraum gesucht (d.h. ihr wird der Name des aktuellen Namensraums vorangestellt) ohne dabei aber ein automatisches Laden. zu versuchen.
- Die Klasse wird im globalen Namensraum gesucht, wieder ohne automatisches Laden zu versuchen.
- Es wird versucht, die Klasse innerhalb des aktuellen Namensraumes automatisch zu laden.
- Wenn keiner der bisherigen Schritte erfolgreich war, so schlägt die Auflösung des Klassennamens fehl.
-
Ein nicht qualifizierter Funktionsname (d.h. ohne ::) wird zur Laufzeit zunächst im aktuellen und dann im globalen Namensraum gesucht.
-
Nicht qualifizierte Konstantennamen werden zunächst im aktuellen Namensraum und dann in der Liste der global definierten Konstanten gesucht.
Siehe auch die vollständigen Regeln zur Namensauflösung.
Deklaration von Namensräumen
07-Sep-2008 03:56
14-May-2008 10: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.
01-Apr-2008 10: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.
