按合同设计
我相信断言在PHP中用得不多.你可以使用它,如果你Design By Contract.
Design by Contract (DbC) or
Programming by Contract is an approach
to designing computer software. It
prescribes that software designers
should define formal, precise and
verifiable interface specifications
for software components, which extend
the ordinary definition of abstract
data types with preconditions,
postconditions and invariants. These
specifications are referred to as
“contracts”, in accordance with a
conceptual metaphor with the
conditions and obligations of business
contracts.
这个tutorial解释了一下.
TDD /单元测试
我建议你按照这些3 simple rules(必读文章)对你的代码(TDD)进行单元测试.
You are not allowed to write any production code unless it is to make a
failing unit test pass.
You are not allowed to write any more of a unit test than is sufficient
to fail; and compilation failures are
failures.
You are not allowed to write any more production code than is
sufficient to pass the one failing
unit test.
You must begin by writing a unit test
for the functionality that you intend
to write. But by rule 2, you can’t
write very much of that unit test. As
soon as the unit test code fails to
compile, or fails an assertion, you
must stop and write production code.
But by rule 3 you can only write the
production code that makes the test
compile or pass, and no more.
If you think about this you will
realize that you simply cannot write
very much code at all without
compiling and executing something.
Indeed, this is really the point.
要练习(训练),你应该使用优秀的PHPUnit框架.你应该阅读这篇Writing Tests for PHPUnit以了解这门学科.