2017 © Pedro Peláez
 

library type

Immuatble object behaviour for PHP

image

simondeeley/type

Immuatble object behaviour for PHP

  • Monday, December 4, 2017
  • by unwarysheep
  • Repository
  • 1 Watchers
  • 0 Stars
  • 297 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 8 Versions
  • 4 % Grown

The README.md

Reusable immutable objects for PHP

License Latest Stable Version Latest Unstable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage, (*1)

This package contains a few reusable classes, interfaces and traits which have been written to provide a basis for creating both immutable and variable objects., (*2)

This package was born out of a love creating beautifully crafted 'boilerplate' code which can be reused again and again in any number of both simple and complex objects. Most of the ideas used in this package revolve around the need to create the most basic of PHP objects - one that is immutable and unchanging. Trouble is, PHP has a fair few 'magic methods' which, although great for method overriding, are less great for ensuring an immutable state. This package aims to solve that problem and provide a number of base classes to allow you to build easy-to-use immutable classes of your own., (*3)

Requirements

  • PHP >= 7.1.0

Installation

composer require simondeeley/type

Usage

Create your own immutable object, (*4)

use simondeeley\ImmutableObject;

class Foo extends ImmutableObject
{
    //...
}

This is the starting point to creating an immutable object. Behind the scenes, the base class of ImmutableObject sets up a few sensible defaults including overriding PHP's magic methods __set and __unset to prevent implicitly setting (or unsetting) of class properties., (*5)

There is one further method that you need to implement in your concrete classes which is getType. This method is inherited from the interface simondeeley\Type\Type which defines one static method getType which should return a string describing the type of the object., (*6)

It might seem strange having this method but it's useful when defining a bunch of objects which might, for example, share the same base class but who's identity you want to differentiate., (*7)

In the example above, you might implement the interface as such:, (*8)

public static function getType(): string
{
    return 'foo';
}

Going Further

As well as ImmutableObject this package also provides a second base class for objects which need to behave like arrays, implementing PHP's built-in ArrayAccess. This behaviour is provided in ImmutableArrayTypeObject which extends ImmutableObject and includes default implementations of offsetSet and offsetUnset., (*9)

Each of these classes uses a few traits which provide the default implementations described above. You can explore these as well as the interfaces in the code which is fully documented and annotated., (*10)

Testing Equality

Oftentimes there is a need to check that two objects are equal. This package provides a TypeEquality interface which describes one method, equals., (*11)

public bool equals ( Type $type [, int $flags ] )

This method takes an object of type simondeeley\Type\Type as an argument. This interface is the base interface that all other 'types' are built on. This method also optionally accepts a further parameter - a bitwise integer flag. This can be used to enforce or ignore certain constraints. Currently, two flags are supported:, (*12)

  • TypeEquality::IGNORE_OBJECT_TYPE, (*13)

    Set this flag to ignore the type of the object which is set by Type::getType., (*14)

  • TypeEquality::IGNORE_OBJECT_IDENTITY, (*15)

    Set this flag to ignore checking that an object points to the same PHP reference, for example by checking the value obtained by invoking spl_object_hash., (*16)

Bitwise operators can be combined by using the pipe '|' operator, for example $foo->equals($bar, TypeEquality::IGNORE_OBJECT_TYPE | TypeEquality::IGNORE_OBJECT_IDENTITY), (*17)

When implementing this method you can choose whether to utilise the bitwise flags but they should be present in your method signature. The body of the method should then perform whatever actions are necessary to compare the two objects to determine equality. An example of this can be found in the simondeeley\Tuple package which uses this package to build an immutable tuple object that can check equality against another tuple., (*18)

The Versions

04/12 2017

1.2.x-dev

1.2.9999999.9999999-dev https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

php immutable behaviour

04/12 2017

dev-master

9999999-dev https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

The Development Requires

php immutable behaviour

04/12 2017

1.2.2

1.2.2.0 https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

php immutable behaviour

30/11 2017

v1.2.1

1.2.1.0 https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

php immutable behaviour

30/11 2017

1.2.0.x-dev

1.2.0.9999999-dev https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

The Development Requires

php immutable behaviour

28/11 2017

dev-docs

dev-docs https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

The Development Requires

php immutable behaviour

27/11 2017

v1.1.0

1.1.0.0 https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

The Development Requires

php immutable behaviour

25/11 2017

v1.0

1.0.0.0 https://github.com/simondeeley

Immuatble object behaviour for PHP

  Sources   Download

GPL-3.0

The Requires

  • php ~7.1

 

The Development Requires

php immutable behaviour