Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

名前空間とReflection

なんとなく試してみました。クラスとクラス定数の関係と同じで、名前空間付きのクラス名をReflectionClassに渡してやればOKですね。ただし、importした名前は使えないようです。

$ php -v
PHP 5.3.0-dev (cli) (built: Jul 15 2008 17:36:47)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2008 Zend Technologies
$ cat reflection_with_namespace02.php
<?php
namespace Foo::Bar;
use Foo::Bar as foo;

require_once 'include.php';

class Baz
{
    const HOGE = 'hogehoge';
}

print_r(new ReflectionClass('Foo::Bar::Baz'));
print_r(new ReflectionClass(__NAMESPACE__ . '::Baz'));

/**
 * importした名前だと名前空間に変換してくれないっぽい
 * PHP Fatal error:
 * Uncaught exception 'ReflectionException' with message 'Class Baz does not exist' in ...
print_r(new ReflectionClass('foo::Baz'));
 */
print_r(new ReflectionClass(new Baz()));


print_r(new ReflectionClass('Hoge::Hoge'));
print_r(new ReflectionClass('hoge::hoge'));
$ cat include.php
<?php
namespace Hoge;

class Hoge
{
    const FOO = 'bar';
}
$ php reflection_with_namespace02.php
ReflectionClass Object
(
    [name] => Foo::Bar::Baz
)
ReflectionClass Object
(
    [name] => Foo::Bar::Baz
)
ReflectionClass Object
(
    [name] => Foo::Bar::Baz
)
ReflectionClass Object
(
    [name] => Hoge::Hoge
)
ReflectionClass Object
(
    [name] => Hoge::Hoge
)
$ 

ところで、名前空間ってcase-insensitiveでしたっけ。。。?