0

What i have leaned in PHP so far that class keyword is reserved that can be used only for creating a PHP class.

However i have seen that in some frameworks such as ZEND they use it in other places like this AlbumController::class.

so why it is used like so and what's the name of this concept ?.

as example :

return [
    'controllers' => [
        'factories' => [
            Controller\AlbumController::class => InvokableFactory::class,
        ],
    ],
jimy razit
  • 135
  • 1
  • 11

3 Answers3

1

I believe this concept is called class name resolution. The example usage you have given looks very much like a definition for a Dependency Injection Container.

With a DI Container, you list your definitions in an array format, like the code you have given, and then when you need the defined class, all of your injections are automatically resolved.

For example, say you have a DI container with the following definitions:

$containerDefinitions = [
    Database\DatabaseAccessorInterface::class => Database\ConcreteDatabaseAccessor::class,
    Data\QueryExecutionHandlerInterface::class => Data\ConcreteQueryExeuctionHandler::class
];

...and then you have the class ConcreteQueryExeuctionHandler constructor defined like so:

public function __construct(DatabaseAccessorInterface $databaseAccessor)
{
    $this->databaseAccessor = $databaseAccessor;
}

...well, when you retrieve the definition QueryExecutionHandlerInterface from the DI container, with something like this:

$queryExecutionHandler = $container->get(Data\QueryExecutionHandlerInterface::class);

...then it will auto-inject the ConcreteDatabaseAccessor during construction of ConcreteQueryExecutionHandler.

This is a very specific example, but I believe the code you have provided is a DI container definition array.

e_i_pi
  • 4,590
  • 4
  • 27
  • 45
1

It is class name resolution. It helps in getting the fully qualified class name, particularly used to get namespaced class.

In zend framework we see namespace used for trivial purposes. Using class name resolution helps getting the class name along with the name space.

For example:

namespace SOMENAME {
    class ClassName {
        //some implementation
    }

    echo ClassName::class;
}

The will output:

SOMENAME\ClassName
Nishanth Matha
  • 5,993
  • 2
  • 19
  • 28
0

Good explanation: https://stackoverflow.com/a/42064777/8518859

PHP documentation: http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name

Mike S
  • 1,636
  • 7
  • 11
  • no sir im not asking about the scope resolution operator im asking about the use of the class keyword with the scope resolution operator in that way – jimy razit Aug 31 '17 at 00:20
  • My bad, updated to better reflect the question. – Mike S Aug 31 '17 at 00:27
  • you are my hero thanks a lot by the way i have searched a lot but wasn't able to find a result because i didn't know the concept – jimy razit Aug 31 '17 at 00:34