Skip to content

Simple reflection-based library to check if a class implicitly matches an interface

License

Notifications You must be signed in to change notification settings

yaronius/php-implicit-interface-check

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP implicit interface checker

Simple reflection-based library to check if a class matches an external interface (i.e. an interface that is not implemented by this class, but probably having the same method signatures).

Wait, but why?

This library was inspired by Go language and its concept of interfaces. In Go interfaces are implemented implicitly, i.e. an object implements an interface when it provides the same method signatures as described in the interface. Whereas in PHP interfaces MUST be implemented explicitly and this fact somehow prevents proper code decoupling.

Let's say we have the following class:

class DeepThought
{
    public function answer(string $life, int $universe, array $everything): int
    {
        return 42;
    }
}

It does not implement any interface. Let us coin an interface for it:

interface DeepThoughtInterface
{
    public function answer(string $life, int $universe, array $everything): int;
}

Actually, the signature of method answer() in the class completely matches the one in the interface. But we can't actually confirm that with any standard PHP tools.

$instance = new DeepThought();
if ($instance instanceof DeepThoughtInterface::class) {
    echo 'Success';
} else {
    echo 'Failure';
}

We will always receive Failure message. And this is sad. But now we can use this library for checking:

use function yaronius\ImplicitInterface\class_complies_with;

$instance = new DeepThought();
if (class_complies_with($instance, DeepThoughtInterface::class)) {
    echo 'Success';
} else {
    echo 'Failure';
}

Now we get Success! Finally!

TODO

  • more tests
  • benchmark performance impact

About

Simple reflection-based library to check if a class implicitly matches an interface

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages