Download the PHP package clj/protocol without Composer

On this page you can find all versions of the php package clj/protocol. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package protocol

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.


All versions of protocol with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package clj/protocol contains the following files

Loading the files please wait ....