2017 © Pedro Peláez
 

library domainobject

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

image

angrybytes/domainobject

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  • Monday, November 3, 2014
  • by naneau
  • Repository
  • 7 Watchers
  • 4 Stars
  • 489 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

DomainObject

This is a simple class that signifies classes that extend it are a DomainObject. In a more practical sense it offers a properties implementation, that is lacking from PHP., (*1)

Why

We believe in a plain old php objects (POPO) for modelling your domain. These objects should hold no logic other than their core values. A DomainObject should have a direct link to an entity in your Universe of Discourse., (*2)

This class helps you do that in a generic way. It has some niceties such as support for accessing your properties through both functions and object property notation. But mostly, it is a strong signal that the class that's extending it is, in fact, a DomainObject., (*3)

Why not simply rely on public variables?

Using simple variables like:, (*4)

class Person
{
    public $name;
}

Works pretty well for most simple properties., (*5)

Imagine the following though:, (*6)

class Person
{
    public $firstName;

    public $lastName;

    public function getFullName()
    {
        return $this->firstName . ' ' . $this->lastName;
    }
}

In order to get the full name for a person (first + last), you need to write a method. Now you have to mix both properties and methods in your API. This is not very consistent and rather inflexible., (*7)

Example

<?php

use Angrybytes\DomainObject;

use \InvalidArgumentException;

class BlogPost extends DomainObject
{
    private $title;

    private $contents;

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        // You can do simple sanity checks in your setters
        if (strlen($title) < 3) {
            throw new InvalidArgumentException('Title should be 3 or more characters long');
        }

        $this->title = $title;

        return $this;
    }

    public function getContents()
    {
        return $this->contents;
    }

    public function setContents($contents)
    {
        $this->contents = $contents;

        return $this;
    }
}

Using this you can:, (*8)

<?php

$post = new BlogPost;

// Set properties
$post
    ->setTitle('This is the title for my blog post')
    ->setContents('foo');

// Retrieve properties using property notation
echo $post->title;
echo $post->contents;

// Retrieve data in array form for easy serialization
$json = json_encode(
    $post->toArray()
);

The Versions

03/11 2014

dev-master

9999999-dev

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  Sources   Download

MIT

The Development Requires

03/09 2014

1.0.3

1.0.3.0

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  Sources   Download

MIT

The Development Requires

05/05 2014

1.0.2

1.0.2.0

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  Sources   Download

MIT

The Development Requires

05/05 2014

1.0.1

1.0.1.0

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  Sources   Download

The Development Requires

05/05 2014

0.0.2

0.0.2.0

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  Sources   Download

The Development Requires

05/05 2014

1.0.0

1.0.0.0

A simple DomainObject implementation that provides simulated properties for PHP < 5.5

  Sources   Download

The Development Requires