PHP interfaces and optional parameters

Interfaces in object-oriented languages like Java, C# and PHP are useful language constructs for ensuring that objects will respond to certain methods. However, unlike Java and C# (until recently), PHP has optional parameters. As it turns out, this feature has an interesting (and a little bit unexpected) effect on how we can implement interfaces in PHP.

Consider a simple interface for making objects comparable:

interface IComparable {
     public function compareTo($object);

Normally, in Java and C# (before 4.0) the only way to implement this interface would be to make a carbon copy of the method signature (save for the name of the parameter) and then fill in the method body.

class NetString implements IComparable {
     public function compareTo($object) {

This satisfies the interface requirements because we need to have a method that responds to a method named compareTo with a single argument. However, things get interesting when we introduce optional parameters. That’s because we can write a method signature that is different from that found in the interface but still satisfies the requirement that the method have the correct name and accept only 1 parameter.

How is that possible? Well, imagine we wanted our NetString class to optionally ignore case when comparing:

class NetString implements IComparable {
     public function compareTo($object, $ignoreCase = false) {

Notice how we slipped in another (optional) argument $ignoreCase? Does this code still satisfy the IComparable interface? Absolutely. Why? Because we can still call compareTo($object) in a meaningful way, and that’s all the matters.

4 Comments so far

  1. [...] This post was mentioned on Twitter by Peter Wooley, Thomas Buck, Hacker News, hkrnws, Technojobz and others. Technojobz said: PHP interfaces and optional parameters [...]

  2. uberVU - social comments on April 24th, 2010

    Social comments and analytics for this post…

    This post was mentioned on Twitter by Technojobz: PHP interfaces and optional parameters

  3. Max Win on April 25th, 2010

    What a surprise. I certainly appreciate the flexibility the optional parameters brings. But one big advantage of interface is that it allows you to change the code without affecting those that call the methods. Imagine I first implemented a function with an optional parameter and other parts of my program call the method with the optional parameter. Later i decided to change my code so that the code no longer deals with the optional parameter. Previous method calls with the optional parameter will still work but not in a way you would expect it.

    eg. compareTo(object, true) will be no different from compareTo(object,false) since the optional parameter will just be ignored. This can be confusing for a different programmer reading the code. He needs to read the implementation to figure out what is really going on. And this totally beats the purpose of encapsulation.

  4. cdmckay on April 25th, 2010

    @Max Win:

    If I understand you correctly, you are saying that you have an issue with the fact that one can include an optional parameter in their implementation of compareTo($object).

    Encapsulation is not violated here. This is because the interface is still fully implemented. The interface only demands that the implementer respond to the call compareTo($object). Thus, people should only expect that call to work.

Leave a reply