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.
名前空間
目次
名前空間の概要
PHP における名前空間は、PHP のライブラリが巨大化したときの スコープの問題を解決するために設計されています。 PHP では、すべてのクラス定義はグローバルです。 したがって、ライブラリの作者がさまざまな公開 API を作成する際には、 他のライブラリの同様の機能との競合に注意する必要があります。 お互いに名前が重複しないよう、一意な名前を選択する必要があるのです。 このとき、通常はクラス名を先頭に付加することで一意な名前を作成します。 たとえばデータベースのクラスなら、その先頭に My_Library_DB を付加するなどといったことです。 ライブラリが巨大化するにつれて、このプレフィックスもどんどん追加され、 非常に長ったらしい名前になってしまいます。
名前空間を使用すると、そのクラスを参照する際に毎回長い名前を使用する必要がなくなります。 コードの可読性を保ったまま、グローバル空間の共用の問題を解決することができます。
名前空間は、PHP 5.3.0 以降で使用可能です。 このセクションの内容は実験的なものであり、変更される可能性があります。
名前空間
yarco dot w at gmail dot com
01-Jul-2008 10:19
01-Jul-2008 10:19
Amir Abiri
26-Dec-2007 02:31
26-Dec-2007 02: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.
