Fake function/method overloading in PHP

Ever needed to call the same function with different number of parameters? Easy right, just use default values:

  1.  

But what if you want to make the first parameter optional?
Here’s a simple technique for doing so it in a transparent way.

  1. span class=”st0″>"param1: $param1, param2: $param2, param3: $param3"

When you run the code you get:

C:\>php -f test.php
param1: 1, param2: 2, param3: 3
param1: , param2: 1, param3: 2
param1: , param2: , param3: 1

The point of this technique is to make it obvious that you’re trying to reassign the parameters and do it with as few lines as possible.

Leave a Reply