PHPackages                             clj/protocol - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. clj/protocol

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

clj/protocol
============

PHP version of clojure protocols

1.0.0(13y ago)427MITPHPPHP &gt;=5.3.0

Since May 23Pushed 13y ago1 watchersCompare

[ Source](https://github.com/Schuck-Johnson/PHP-Protocol)[ Packagist](https://packagist.org/packages/clj/protocol)[ RSS](/packages/clj-protocol/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

\#Clojure Protocols for PHP

\##What is a protocol?

See the Details.md file for more information

\##Creating a protocol

1. Create an interface with the methods you want the protocol to implement. The first parameter of each method in the interface needs to be the invoking object. So create your interface like this

    ```
     interface ILookup
     {
         public function get($context, $key, $default = null);
         public function exists($context, $key);
     }

    ```

    not like this

    ```
     interface ILookup
     {
         public function get($key, $default = null);
         public function exists($context, $key);
     }

    ```
2. Create the protocol implementing the interface

    ```
     class Lookup extends \Clj\AProtocol
     {
         public function get($object, $key, $default = null)
         {
             return $this->__getObject__($object)->get($object, $key, $default);
         }
         public function exists($object, $key)
         {
             return $this->__getObject__($object)->exists($object, $key);
         }
     }

    ```

    the \_\_getObject\_\_ method finds the objects type and returns the concrete implementation of the protocol
3. Create the concrete implementations of the protocol (in my code I have the type name of the implementation prefixed by a P (i.e. PNull for a null or PDateTime for a DateTime object)

    ```
     class PArray extends \Clj\AProtocolInstance implements ILookup //Lookup for arrays
     {
         public function get($context, $key, $default = null)
         {
             if (isset($context[$key]) || (array_key_exists($key, $context)))
             {
                 return $context[$key];
             }
             return $default;
         }

         public function exists($context, $key)
         {
             return (isset($context[$key]) || (array_key_exists($key, $context)));
         }
     }

     class PNull extends \Clj\AProtocolInstance implements ILookup //Lookup for null
     {
         public function get($context, $key, $default = null)
         {
             return $default;
         }

         public function exists($context, $key)
         {
             return false;
         }
     }

    ```

\##Using the Protocol manager

Protocols are created and extended using the Protocol manager class. To add a protocol you pass in a protocol object

```
$lookup = new Lookup();
$protocol_manager->add($lookup);

```

To add types to the protocol you can use either the extendType or extendProtocol methods

```
//Extends a type to an array of protocols
$protocol_manager->extendType(\Clj\IProtocol::PROTOCOL_NULL, array('ILookup' => new PNull()));
//Extends the protocol to an array of types
$protocol_manager->extendProtocol('ILookup', array(\Clj\IProtocol::PROTOCOL_NULL => new PNull(),
    \Clj\IProtocol::PROTOCOL_ARRAY => new PArray()));

```

For retrieving the extended protocols you can use either getProtocol or getProtocolReference. Here's an example of how they are used

```
$lookup = new Lookup();
$protocol_manager->add($lookup);

$protocol_manager->extendType(\Clj\IProtocol::PROTOCOL_NULL, array('ILookup' => new PNull()));
//Gets the lookup protocol that only works for null types
$extended_lookup = $protocol_manager->getProtocol('ILookup');
$lookup_reference = $protocol_manager->getProtocolReference('ILookup');

$protocol_manager->extendType(\Clj\IProtocol::PROTOCOL_ARRAY, array('ILookup' => new PArray()));

$extended_lookup->get(null, 'foo', 'bar'); //Returns 'bar'
$extended_lookup->get(array('foo' => 'bar'), 'foo', 'bar'); //Throws excpetion

$lookup_reference->getRef()->get(null, 'foo', 'bar'); //Returns 'bar'
$lookup_reference->getRef()->get(array('foo' => 'BAZ'), 'foo', 'bar'); //Returns 'BAZ'

```

In the example getProtocol returns the lookup protocol as it exists at the time it was called (only extended for the null type). Any extensions of the protocol done later on (extending the lookup protocol to arrays) are not added to `$extended_lookup`.

In contrast getProtocolReference returns a reference to a protocol similar to `$a =& $b` only this reference is read only and accessed by the getRef method. This allows access to all the extensions of the protocol not just the ones that existed when the reference was created.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

2

Last Release

5098d ago

### Community

---

Top Contributors

[![Schuck-Johnson](https://avatars.githubusercontent.com/u/847171?v=4)](https://github.com/Schuck-Johnson "Schuck-Johnson (34 commits)")

### Embed Badge

![Health badge](/badges/clj-protocol/health.svg)

```
[![Health](https://phpackages.com/badges/clj-protocol/health.svg)](https://phpackages.com/packages/clj-protocol)
```

###  Alternatives

[danielstjules/php-pretty-datetime

Generates human-readable strings for PHP DateTime objects

5791.9k](/packages/danielstjules-php-pretty-datetime)[magepal/magento2-customeraccountlinksmanager

Customer Account Links Manager for Magento2 allows you to quickly and easily remove unwanted links from customer account dashboard

4084.9k](/packages/magepal-magento2-customeraccountlinksmanager)[nystudio107/craft3-multi-environment

Efficient and flexible multi-environment config for Craft 3 CMS

7218.3k5](/packages/nystudio107-craft3-multi-environment)[xi/collections

Functional, immutable and extensible enumerations and collections for PHP 5.3.

26100.6k1](/packages/xi-collections)[psalm/attributes

A collection of PHP 8 Attributes that Psalm can understand

19120.0k2](/packages/psalm-attributes)[kolirt/laravel-openstreetmap

Package for openstreetmap.org

2332.5k](/packages/kolirt-laravel-openstreetmap)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
