Code Monkey home page Code Monkey logo

php-value-objects's Introduction

PHP Value Objects Build Status

This is a proof-of-concept and very much a work-in-progress

What Are Value Objects?

A value object is simply an object that is meant to have minimal functionality and retains a single value. In fully object oriented languages all values are objects, but since PHP is not fully OO we don't have this functionality. This library is an example of a possible implementation of value objects in PHP

Why Do We Need Them?

We don't! But that's not the point. Value Objects can be incredibly useful. In some languages it's possible to extend other primitives, so for example you could create an email address object which extends a string. An email address is just a string, so all string functions should work on an email address object. The only difference between a string and an email address is that the email address has very specific rules about what is and isn't acceptable.

In PHP we will often pass a string around which contains an email address, but we never know that it IS an email address unless we validate it at that step. Unfortunately that often leads to us needing to validate at multiple steps to be sure that a) it was an email address in the first place and b) that it's still an email address.

So this is where Value Objects (herein called simply VOs) come in. If we create a new email address object we can pass it around our code and type hint it in our arguments. For example:

function echoEmail(EmailAddress $em)
{
    echo $em;
}

There's no way that this won't be an email address now. That's incredibly useful because we never need to validate, simply type hint the argument in our functions, class methods etc.

What Happens If The Value Changes?

In this implementation, it can't. The object is immutable. That is, you can set it in the constructor, and you can retrieve the value, but you can't ever change the value. You can replace it, but you'll need to replace it with another new email address object and that will also have to be validated.

What If I Don't Like Your Validation?

I don't expect you to. I implemented some simple validation but it's only an example. You can add your own validator and as long as it implements the PVO\Validators\Interfaces\Validator interface then you can write your own. You simply need to return true if validation is successful and throw PVO\Exceptions\InvalidValueException if it isn't.

Why throw an exception? Because if your value isn't appropriate then I need something more dramatic than just returning a bool, for example. This needs to be caught and handled properly. At the moment the VO constructor itself will throw a new PVO\Exceptions\InvalidValueException itself but I'll be sorting this out soon :)

There Aren't Many Implementations. What Use Is This?

This wasn't meant as a serious library for you to implement in your application, though I would be quite pleased if it does grow to such a state. However, the point was for people to fork it, add their own, and then submit a PR back to me so that we can add your VOs into the library. Maybe one day it will become useful to others

How To Install

Installation is via Composer, of course. Simply composer require antnee/php-value-objects from within your project or add this to your composer.json and run composer update:

{
    "require": {
        "antnee/php-value-objects": "*"
    }
}

and you will have the latest dev version. I'll tag stable releases if we ever get that far

php-value-objects's People

Contributors

antnee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hendeavors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.