m2f
connecting communities

Technology

v2.0 Development • Technology • Development Status

Test Driven Development

This is the big one! This technique has completely changed the way I write PHP. The concept is very simple, but takes quite a long time to get used to. Rather than try and explain it myself, I’ll let an expert take over. Please read this introduction, even if you’re sceptical. The idea is that you use Tests to specify what your application should do. Write the test first, then the code to satisfy the test. Write no more code than is necessary. In doing this, you get some amazing bonuses:

  • all your code is covered by Unit Tests. If a change in code makes a class behave differently, a tests will fail and you will notice it
  • You have no more code than you specifically need - no ‘code bloat’ with unused functionality
  • If a bug is found, write a test that fails because of the bug. When the test passes, the bug is solved, and should not reappear because of the test

I use Simpletest as my TDD framework. I store my tests in well-organised directories, and run the whole test suite at least once a minute as I develop. My tests are grouped into Unit Tests (testing each class behaves as it should as a separate entity), Acceptance Tests (check groups of classes work well together) and Web Tests (check the Admin Control Panel behaves as expected, producing the correct XHTML).

Advanced PHP 5 OOP techniques

Well, if you’ve used them, you won’t think they’re that advanced. But I have now fully embraced many of the additional features that PHP 5 brings to OOP. I’m using Exceptions to trigger errors now, so that my classes don’t need to return true or false any more. I’m using Interfaces to enforce certain design from my classes, rather than Inheritance. I’m making use of the SPL (Standard PHP Library) to add useful features to my objects with very little code.

External Libraries

Since we’ve left PHP 4 behind, I have now been able to bring on board some of the cutting-edge PHP libraries. The two big ones I’ve gone for so far are the Zend Framework and Doctrine ORM.

Zend is kind of a two-in-one solution in this context. I’m using some parts of the library (in a similar way to PEAR in earlier m2f versions) for things like Mbox access, Caching, Configuration files, etc. I’m also using Zend’s MVC framework. Although I’d written a pretty good one for Alpha 3, I was learning on the job, and just realised that Zend supplied everything I’d done, only better, plus 500% more!

Doctrine again replaces functionality I’d started to build into Alpha 3. It’s ORM, which basically means we just do this:

$chain = new M2f_Chain; 
 
$channel = new M2f_Module_Channel_Phpbb;
$channel->phpbbRoot = '../phpBB3';
 
$chain->channels[] = $channel;
$chain->save();

and our chain is saved in the database. Doctrine uses very simple configuration files to specify the design of database tables, which means both that they are DB-engine-agnostic (great for supporting as many users as possible), and that developers will be able to develop 3rd-party channels and filters with ease.