PHP Method Overloading: Why?

So some people I know really like to use PHP method overloading. In case you program in pretty much any language other than PHP, let me explain what PHP considers method overloading using this (very simplified) example code:

<?php

	class bigAnimal{
		function __call($name, $arguments){
			return $name;
		}

		function displayName($name, $arguments){
			return $name;
		}
	}

	$animal = new bigAnimal();
	$animal->monkey();
	$animal->displayName($monkey);

?>

So, in PHP, method overloading means that if bigAnimal::monkey() doesn’t exist, it will look for bigAnimal::__call(), and pass the name of the method (monkey) and any arguments to the __call() method. Seems handy at first, doesn’t it? If you want a bunch of similar methods to do the same thing, or have a default method, then you can easily set this up.

But why? The only thing you are getting out of this arrangement is the ability to be lazy and not name one function, at the expense of speed, readability, searchability, maintainability, and I’m sure there is another -ability that it costs you, but it is getting late so I’m not sure…

For example, change __call() to displayName(). Now, all you need to do is stay aware of what methods you’ve declared in your class, and if you want to use one that doesn’t actually exists, then call bigAnimal::displayName(‘monkey’). You can even pass in your arguments if you want.

But maybe you guys are smarter than me. Can someone explain what the benefit of this is supposed to be?

Leave a Reply

Your email address will not be published. Required fields are marked *