Full name of the interface

Is there a way to get the fully qualified interface name similar to MyClass::class?

For example:

namespace Example\Tests;

use Example\Interfaces\InputInterface;
...

class CommandTest ...
...
public function createInputMock()
{
    // I want to replace next string with something similar to MyClass::class
    $this->getMockBuilder('Example\Interfaces\InputInterface')
...

Thanks.

+4
source share
2 answers

Name resolution ::classcan work with any imported namespaces: classes, interfaces, functions, ...

namespace A\B\C {
    interface Interface_Bar {}
    function Function_Foo() {}
    function Function_Foo_Bar() {}
    const Const_BARFOO = 123;
}

namespace {
    use A\B\C\Interface_Bar;
    use A\B\C;
    use Undefined\Classes\UndefinedClass;
    use function A\B\C\Function_Foo_Bar;
    use const A\B\C\Const_BARFOO;

    echo Interface_Bar::class, "\n"; // print A\B\C\Interface_Bar
    echo C\Function_Foo::class, "\n"; // print A\B\C\Function_Foo
    echo C\Const_BARFOO::class, "\n"; // print A\B\C\Const_BARFOO
    echo UndefinedClass::class, "\n"; // print Undefined\Classes\UndefinedClass

    echo Function_Foo_Bar::class, "\n"; // print Function_Foo_Bar <- warning
    echo Const_BARFOO::class, "\n"; // print Const_BARFOO <- warning
}
+7
source

If I'm right, you cannot switch to PHP 5.5 with a note ::class, so you want to have something like this in your 5.4 or earlier vesrion.

So the short answer is no, there is no way.

The lack of this functionality in previous versions of PHP is what made kernel developers add ::classin PHP 5.5.

, : get_class , .

+1

Source: https://habr.com/ru/post/1617719/


All Articles