Let's say I have a class in PHP with a method that serialises data. Being English, I use the English spelling of -ISE.
class Foo
{
public function serialise($data)
{
return json_encode($data);
}
}
Let's also say that I have an American developer on my team who tries to "serialize" the data (-IZE).
$foo = new Foo();
$foo->serialize(['one', 'two', 'three']);
// PHP Fatal error: Uncaught Error: Call to undefined method Foo::serialize()
The obvious way to solve this is to alias the method. My instincts are to create another method with the American spelling and simply pass the same parameters.
class Foo
{
// ...
public function serialize($data)
{
return $this->serialise($data);
}
}
But this has a couple of downfalls:
- I have to match the parameters and remember to update both methods if there's ever an update (error prone).
- Additional function call for American developers means the code is less efficient.
- Modifying one in a sub-class doesn't necessarily update the other.
My question is: is there a better way to alias a class method in PHP? A way that can get around the concerns that I have?