Archive for March 2011

Week 13, year 2011

  • Representing Money in PHP, Fowler-style - Whenever working with values in object oriented programming, it’s often a good idea to wrap them in a ValueObject. Money is a perfect candidate for a ValueObject: When talking about money, numbers are meaningless if they are not combined with a currency. I’ve been using a very simple version of the Money pattern as described in Martin Fowler’s PoEAA. I couldn’t find a PHP implementation anywhere, so I decided to make my own little open source library for it. You can find it on my GitHub account (where else?). Immutability An important aspect of ValueObjects is their immutability: Let’s say Jim and Hannah both want to buy a copy of book priced at EUR 25. [Mathias Verraes]
  • Random thoughts on using Git - A couple of weeks ago, I switched from Subversion to Git for a couple of smaller projects. Below are some of my findings. Switching from Subversion Git is a little harder to understand than Subversion, because of it’s lack of a centralized repository, and because it has more concepts you need to understand. But if you already understand SVN well, it’s very manageable. I’ve no idea how the learning curve compares to SVN for a complete newbie to version control. If you are on the fence, there are two things that I wish I knew about sooner: Git Immersion only takes an hour or so, but you’ll understand so much more of git than when using conventional books or tutorials. [Mathias Verraes]
Permalink | From 28 March 2011 to 03 April 2011 | Last updated on: Mon, 7 Jun 2021 09:18:52 GMT

Week 12, year 2011

  • Accessing private properties from other instances - In PHP, when a property or method is marked private, it can only be accessed from within that class. That includes other instances of the same class. This may seem counter-intuitive at first, because we are used to dealing with instances of classes. The visibility operator however works not on object-level, but on class level. An example: <?phpclassFoo{private$private;publicfunction__construct($value){$this->private=$value;}publicfunctiongetOther(Foo$object){return$object->private;}}$foo1=newFoo('foo1');$foo2=newFoo('foo2');echo$foo1->getOther($foo2);// outputs 'foo2' This should make it clear that both instances of Foo have access to each other’s private properties. What practical use does this have? A great candidate for this are Value Objects. [Mathias Verraes]
  • Keep your controllers thin with Doctrine2 - Doctrine2 does such a nice job abstracting everything related to the database, that you might be tempted to do everything else in your controllers. Say we have a Bug entity: <?php/** @Entity */classBug{/** @Column(type="integer") */private$id;/** @Column(length=50) */private$status;//. . . } To get a list of fixed bugs, we get the Bug repository from the EntityManager and ask for a list of Bugs where status equals ‘fixed’. <?php<?php// $em instanceof Doctrine\ORM\EntityManager$fixedbugs=$em->getRepository('Bug')->findBy(array('status'=>'fixed')); That’s easy enough. [Mathias Verraes]
Permalink | From 21 March 2011 to 27 March 2011 | Last updated on: Mon, 7 Jun 2021 09:18:52 GMT

Week 11, year 2011

  • Interface discovery with PHPUnit’s Mock objects - PHPUnit provides some great features to create mock objects. The idea is that when you are testing code that depends on another class, you provide the object with a mock instance of that class, instead of a real object. That way, you are making sure that your test will only fail if the system under test is broken, and not if one of it’s dependencies is broken. You could simply write a mock class and instantiate it, but PHPUnit can generate them for you. The PHPUnit documentation doesn’t explicitly state this, but you can also create mock objects from interfaces. This makes a lot of sense if you think about it. [Mathias Verraes]
Permalink | From 14 March 2011 to 20 March 2011 | Last updated on: Mon, 7 Jun 2021 09:18:53 GMT