Closures with validation rules for domain object properties It is nice to be able to set your data in a domain object like so:

$order-&gt;setId($id);
$order-&gt;setCustomer($customer);
$order-&gt;setItems($items);


And then be able to do

if( !$order-&gt;validate() ) {
    // Invalid data
}


Which will validate all the set data and tell you if it was valid or not. I have an implementation of this I came up with but it is sort of messy and limited to only certain common validation rules like checking the type, length/range and format of the data.

I have been doing a lot of Javascript lately and I like the idea of the anonymous functions and I just found out you can do them in PHP 5.3+ and you can use the $this keyword from PHP 5.4.

abstract class AbstractEntity {

    private $id = 0;
    private $validations = array();

    public function setId($id) {
        $this-&gt;id = $id;

        // A function to validate the Id
        $this-&gt;addValidation( function() {
            return ( $this-&gt;id &gt; 0 );
        });
    }

    public function getId() {
        return $this-&gt;id;
    }

    public function validate() {
        foreach($this-&gt;validations as $validation) {
            if( $validation() === false ) {
                return false;
            }
        }

        return true;
    }

    protected function addValidation($function) {
        $this-&gt;validations[] = $function;
    }

}


The Order class:

class Order extends AbstractEntity {

    private $customer;
    private $items = array();
    private $status;

    public function setCustomer(Customer $customer) {
        $this-&gt;customer = $customer;

        // Make sure the customers account is active
        $this-&gt;addValidation( function() {
            return ( $this-&gt;customer-&gt;isActive() );
        });
        // Do as many complex validations as needed
    }

    public function setStatus($status) {
        $this-&gt;status = $status;

        // The status has to be 'Pending' or 'Complete'.
        $this-&gt;addValidation( function() {
            return ( ($this-&gt;status === 'Pending') || ($this-&gt;status === 'Complete') );
        });

}


Doing it this way validations of all different levels of complexity can be done. I was thinking of adding in another parameter to the addValidation() method so you can group validation rules together so they execute in a certain order.

It needs extra functionality like setting error messages but that should not be too hard to implement.

I was also thinking of maybe making the addValidation() method public so you can add validations outside of the class that will execute when the validate() method is called. This could be handy in some scenarios.

Is this a good or bad idea? I would like to see some opinions before I go and change all my code to using this sort of implementation.
my two cents :


I think tying validation activation to setters is not really good. What if you never set any value on your object ? It will be considered valid ! And what happens if you set the property more than once ?
Some implementations use external validator objects, which allow more flexibility (validation rules often vary, according to which user requests saving for instance). When you call validate(), you either pass a validator instance or let the object use a default validator. As a side note, coming from Rails / ActiveRecord, I can tell that complex projects often do not turn well when your domain object's lifecycle is so tightly tied to a single validation scheme !
Speaking DDD lingo, some of your problems might be efficiently solved using Value objects. For instance, your Id property could be a value object that has a isValid() method (it would be responsible for its own validation). Wether or not this is a good idea depends on your domain.