Will Pillar

Read this first

The Open-Closed Principle: Some Intepretations

I am a fan of the SOLID principles, I think if you use them as guidance when you’re doing OOP you can deliver flexible, testable and scalable solutions.

That said, one of the principles that I haven’t quite got totally to grips with is the Open-Closed Principle.

I was talking with a colleague and they had a very different interpretation than I did. Let’s have a look at each of them.

My Interpretation

Take this class as an example (stolen from the Laracasts video).

class AreaCalculator
{
    public function calculate(Square $square)
    {
        return $square->length * $square->height;
    }
}

This class calculates the area of a Square, but it’s currently not very flexible and it violates the OCP because we’d have to modify the internals of the class every time we need to extend this class to calculate the areas for other shapes. Like this:

class AreaCalculator
{
    public
...

Continue reading →


DBAD: Constructors in PHP interfaces

Interfaces are a wonderful thing and they allow developers to build brilliantly flexible things with defined boundaries and contracts. However, like any good thing they can be misused

Earlier this week I was messing around with a Zend Framework 1 application trying to make it do something a bit differently by extending some of its core classes. It was all going so well until I setup my new sub-class on the front controller and then got an error telling me that my class did not match the interface one if its parent classes was implementing. After having a little dig, I discovered that the interface in question had put a constructor in the interface, meaning that any implementing class had to have the same constructor declaration.

interface Foo
{
    public function __construct(Bar $bar);

    public function doSomethingWithBar();
}

I wanted to inject a couple of my own dependencies...

Continue reading →