Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

Protocol-Lib

A library for runtime checking of protocols

Subscribe to updates I use Protocol-Lib


Statistics on Protocol-Lib

Number of watchers on Github 29
Number of open issues 0
Average time to close an issue 5 days
Main language PHP
Average time to merge a PR 4 days
Open pull requests 0+
Closed pull requests 0+
Last commit almost 5 years ago
Repo Created over 6 years ago
Repo Last Updated almost 2 years ago
Size 128 KB
Organization / Authorircmaxell
Contributors2
Page Updated
Do you use Protocol-Lib? Leave a review!
View Protocol-Lib activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating Protocol-Lib for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

ProtocolLib

This is a simple library to allow for checking of GO-style interfaces in PHP.

Basically, it checks if an object would implement an interface (meaning all methods and signatures match) without having to actually implement it.

Examples

class Foo {
    public function bar($a) {}
}

interface Bar {
}
interface Bar2 {
    public function bar();
}
interface Bar3 {
    public function bar($a);
}
interface Bar4 {
    public function bar($b);
}
interface Bar5 {
    public function bar(a $a);
}

When using our helper, checks would look like this:

use ProtocolLib\ProtocolHelper;

$foo = new Foo;

var_dump(ProtocolHelper::doesImplement($foo, 'Bar'));
// True - Empty interfaces *always* are implemented

var_dump(ProtocolHelper::doesImplement($foo, 'Bar2'));
// False - Different Number Of Parameters

var_dump(ProtocolHelper::doesImplement($foo, 'Bar3'));
// True

var_dump(ProtocolHelper::doesImplement($foo, 'Bar4'));
// True - Parameter name doesn't matter

var_dump(ProtocolHelper::doesImplement($foo, 'Bar5'));
// False - Typehint is different

var_dump(ProtocolHelper::doesImplement($foo, 'Foo'));
// LogicException Invalid Interface - Because "Foo" is not an interface

It's as simple as that!

Protocol-Lib list of languages used
Other projects in PHP