
Originally Posted by
been
syntactic validation
I think we are comming from totally different angles on this, can you explain what you mean by "syntactic validation"?

Originally Posted by
been
one is about validating user input, the other is about making sure that an object doesn't go in an invalid state.
Can you explain the difference? From my perspective, those two things are really the same, because user input changes the state of objects. If that new state is invalid, I can spit errors back at the user.

Originally Posted by
been
Sorry, but I fail to see how one would lose polymophism?
Totally agreeing on the generic rules, I'd assemble them in different Validators and have $validator->validate($something); (which is why I fail to see how you'd lose polymophism)
Does that mean you have a User and a UserValidator? Or do you just have a generic Validator which you initialize when you need it like in Chris's example?
Taking "the" example for polymophism:
PHP Code:
class Circle {
function Circle($radius) { ... }
function area() {
return PI * $this->radius * $this->radius;
}
}
class Square {
function Circle($length, $breadth) { ... }
function area() {
return $this->length * $this->breadth;
}
}
$shape = new Circle(42);
And later, when you want to find the area, we use polymophism:
PHP Code:
echo $shape->area();
If we take the area calculation outside of the Shape classes, much like talking the validation outside of the User class, you end up these two classes:
PHP Code:
class Circle {
function Circle($radius) { ... }
}
class Square {
function Square($length, $breadth) { ... }
}
$shape = new Circle(42);
Then, when we want to find the area, you need these classes:
PHP Code:
class CircleAreaCalculator {
function calculate($circle) {
return PI * $circle->radius * $circle->radius;
}
}
class SquareAreaCalculator {
function calculate($square) {
return $square->length * $square->breadth;
}
}
class Calculator {
function setCalculator($calculator) { ... }
function calculate($shape) {
return $this->calculator->calculate(shape);
}
}
and this code to calculate the area:
PHP Code:
$calculator = new Calculator();
$calculator->setCalculator(new CircleAreaCalculator()); //**
echo $calculator->calculate($shape);
The line I've marked with ** is the problem line. If we assume that due to polymophism, $shape could either be a Circle or a Square, then we can't know whether we need a CircleAreaCalculator or a SquareAreaCalculator, but we have to write down one of the two, so we can't have polymophism.
The trick would be to pass a $shape_area_calculator along with $shape ... and rather than passing two variables you might like to make it into one variable, just to make life easier ... so you might have $shape->area_calculator instead ... but then you only ever need to access it through $shape->area() so lets make $shape->area_calculator private ... and then you notice that you've got a bunch of infrastructure code you don't need anymore .. you trim it down ... then you are back at the code at the top of this post.
Well, that's how I see it working out 
Later,
Douglas
Bookmarks