Validating a User via Save Method I'm fairly new to object oriented programming here and am trying to get my head around the best way to go about creating Users and Validating them in the system.

My first attempt is below and my main questions apart from does it all seem correct is in regards passing a validator class to the save function and pros/cons to this.

A lot of the functionality has been stripped back to enable you to read it a bit easier, including removing any getters and setters but please assume they are there:

User Model

class User implements Validateable {

    protected $name;
    protected $email;
    protected $validator;

    // getters and setters for name, email, validator here

    public function save( Validator $validator ) {

        $this-&gt;validator = $validator;        
        if( $this-&gt;validator-&gt;validate( $this ) )
            return true;

        return false;

    }

    public function getRules() {

        return [ 
            'name' =&gt; new NameValidator( $this-&gt;name ), 
            'email' =&gt; new EmailValidator( $this-&gt;email )
            ];

    }

}


Abstract Validator

abstract class ValidatorAbstract {

    protected $errors;

    public function getErrors() {
        return $this-&gt;errors;
    }

}


Name Validator

class NameValidator extends ValidatorAbstract {

    protected $name;
    protected $errors;

    public function __construct( $name ) {
        $this-&gt;name = $name;
    }

    public function validate() {

        if( empty( $this-&gt;name ) ) 
            $this-&gt;errors[] = 'Invalid Name';

        if( $this-&gt;errors )
            return false;

        return true;

    }

}


Email Validator

class EmailValidator extends ValidatorAbstract {

    protected $email;
    protected $errors;

    public function __construct( $email ) {
        $this-&gt;email = $email;
    }

    public function validate() {

        if( !filter_var( $this-&gt;email, FILTER_VALIDATE_EMAIL ) ) :
            $this-&gt;errors[] = 'Invalid Email';
        endif;

        if( $this-&gt;errors )
            return false;

        return true;

    }

}


User Validator

class UserValidater implements Validator {

    protected $data;
    protected $rules;
    protected $errors;

    public function validate( Validateable $obj ) {

        $this-&gt;rules = $obj-&gt;getRules();

        foreach( $this-&gt;rules as $rule ) :

            if( !$rule-&gt;validate() )
                $this-&gt;errors[] = $rule-&gt;getErrors();

        endforeach;

        if( $this-&gt;errors )
            return false;

        return true;

    }

    public function getErrors() {

        return $this-&gt;errors;

    }

}


Validator Interface

interface Validator {

    public function validate( Validateable $data );
    public function getErrors();

}


Validateable Interface

interface Validateable {

    public function getRules();

}


And finally some example code using all of the above:

$user = new User;
$user-&gt;setName( 'Peter' );
$user-&gt;setEmail( 'peter@email.com' );

if( $user-&gt;save( new UserValidater ) ) :
    // do some stuff
else :
    $errors = $user-&gt;getValidator()-&gt;getErrors();
    // do some stuff with the errors
endif;
It's better to separate the validation from the save operation and to use a constraint object to setup the rules which be applied to the entity. So you get a better separation of concern, which is one of the keys to good OOP architecture. I propose something like this: 

$user = new User;
$user-&gt;setName( 'Peter' );
$user-&gt;setEmail( 'peter@email.com' );
$validator = new Validator(new UserConstrainte)); 
if( $validator-&gt;validate($user)){
    $databaseStuff-&gt;save($user)
} else {
    $error = $validator-&gt;getErrors()
}


I advise you to check the validator component from symfony2: https://github.com/symfony/Validator which is really well designed.