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

search for in the

Namespaces verwenden: Grundlagen> <Unter-Namespaces deklarieren
[edit] Last updated: Fri, 17 May 2013

view this page in

Mehrere Namespaces in der selben Datei definieren

(PHP 5 >= 5.3.0)

Es können auch mehrere Namespaces in der selben Datei definiert werden. Es gibt hierfür zwei mögliche Schreibweisen:

Beispiel #1 Mehrere Namespaces definieren, einfache Kombinationssyntax

<?php
namespace MyProject;

const 
CONNECT_OK 1;
class 
Connection /* ... */ }
function 
connect() { /* ... */  }

namespace 
AnotherProject;

const 
CONNECT_OK 1;
class 
Connection /* ... */ }
function 
connect() { /* ... */  }
?>

Diese Syntax ist nicht die empfohlene Syntax, um mehrere Namespaces in einer einzigen Datei zusammenzuführen. Stattdessen wird die geklammerte Syntax empfohlen.

Beispiel #2 Mehrere Namespaces definieren, geklammerte Syntax

<?php
namespace MyProject {

const 
CONNECT_OK 1;
class 
Connection /* ... */ }
function 
connect() { /* ... */  }
}

namespace 
AnotherProject {

const 
CONNECT_OK 1;
class 
Connection /* ... */ }
function 
connect() { /* ... */  }
}
?>

Es wird stark von der Programierpraxis mehrere Namespaces in einer Datei zu definieren abgeraten. Der wichtigste Einsatzzweck für diese Möglichkeit ist es, mehrere PHP-Skripte in der selben Datei zusammenzuführen.

Um Code ohne Namensräume mit solchem mit Namensräumen zusammenzuführen, wird nur die geklammerte Syntax unterstützt. Globaler Code sollte in einem Namespace-Statement ohne Namespace eingeschlossen werden:

Beispiel #3 Mehrere Namespaces und Code ohne Namespace deklarieren

<?php
namespace MyProject {

const 
CONNECT_OK 1;
class 
Connection /* ... */ }
function 
connect() { /* ... */  }
}

namespace { 
// global code
session_start();
$a MyProject\connect();
echo 
MyProject\Connection::start();
}
?>

Es darf kein PHP-Code außerhalb der Namespace-Klammern existieren, abgesehen von einem beginnenden declare-Ausdruck.

Beispiel #4 Mehrere Namespaces und Code ohne Namespace deklarieren

<?php
declare(encoding='UTF-8');
namespace 
MyProject {

const 
CONNECT_OK 1;
class 
Connection /* ... */ }
function 
connect() { /* ... */  }
}

namespace { 
// globaler Code
session_start();
$a MyProject\connect();
echo 
MyProject\Connection::start();
}
?>



add a note add a note User Contributed Notes Mehrere Namespaces in der selben Datei definieren - [2 notes]
up
9
kothnok at gmail dot com
1 year ago
"use" statements are required to be placed after the "namespace my\space" but before the "{".
e.g.

<?php
namespace foo\bar;
use
my\space\MyClass;
{

 
// place code here

} // end of namespace foo\bar

namespace another\bar;
use
my\space\MyClass;
use
my\space\AnotherClass;
{

 
// place code here

} // end of namespace another\bar
?>
up
-4
anders at ingemann dot de
3 years ago
Apparently you will have to define namespaces using curly brackets enclosing theclasses, if you want doxygen to pick them up.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites