I just finished watching this informal talk by J.B. Rainsberger with Corey Haines on an evolutionary design pattern that Rainsberger uses. He says that it’s about removing duplication and bad names, to me it’s a simple way to go about enforcing modular design and implementing your design in a way that very strongly matches the MVC pattern.
The talk is about 10 minutes long but worth a listen. He gives a basic example that has common analogues in most areas of application programming.
In PHP4 objects were passed by value, it’s probably the intuitive way to deal with variables for a beginner and in a language where objects are not first class. However in PHP5 this has been changed and now objects are passed by reference, this stung me when writing some tests recently.
public function testNameIsUnique() {
$test1 = $this->BuildValidDiscountType();
$test2 = $test1;
$test1->Save();
$this->assertTrue($test1->id > 0);
$this->setExpectedException('DiscountTypeException');
$test2->Save();
}
This code shouldn’t have worked as far as I was concerned, in fact I was expecting an exception to be raised. Instead it was working and after a little debug tour I found that my $test1 = $test2 line was causing $test2 to be a reference to $test1, not what I wanted. This caused my update method to be triggered instead and of course the name is still unique.
It only required a small change to that line, using the clone specifier :
$test1 = clone $test2;
After that everything went as expected and I knocked off another test, and added to my PHP knowledge.