1

以下のコードのように、useキーワードのポイントは何ですか?use FooInterface;

namespace Vendor\Package;

use FooInterface;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class Foo extends Bar implements FooInterface
{
    public function sampleFunction($a, $b = null)
    {
        if ($a === $b) {
            bar();
        } elseif ($a > $b) {
            $foo->bar($arg1);
        } else {
            BazClass::bar($arg2, $arg3);
        }
    }

    final public static function bar()
    {
        // method body
    }
}

それって余計なことじゃないですか?

4

1 に答える 1

3

その句がなければ、コードは次のようになります。

class Foo extends Bar implements \FooInterface
                                 ^

先頭のバックスラッシュに注目してください。名前空間にいるために必要です。そのため、グローバル名前空間への参照は、インポートされていない限りVendor\Package、a で始まる必要があります。\

これはマニュアルでも説明されています。

于 2013-08-22T05:11:43.303 に答える