PHP Method Overloading: Why?

So some peo­ple I know really like to use PHP method over­load­ing. In case you pro­gram in pretty much any lan­guage other than PHP, let me explain what PHP con­sid­ers method over­load­ing using this (very sim­pli­fied) exam­ple 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 over­load­ing means that if bigAnimal::monkey() doesn’t exist, it will look for bigAnimal::__call(), and pass the name of the method (mon­key) and any argu­ments to the __call() method. Seems handy at first, doesn’t it? If you want a bunch of sim­i­lar meth­ods to do the same thing, or have a default method, then you can eas­ily set this up.

But why? The only thing you are get­ting out of this arrange­ment is the abil­ity to be lazy and not name one func­tion, at the expense of speed, read­abil­ity, search­a­bil­ity, main­tain­abil­ity, and I’m sure there is another –abil­ity that it costs you, but it is get­ting late so I’m not sure…

For exam­ple, change __call() to dis­play­Name(). Now, all you need to do is stay aware of what meth­ods you’ve declared in your class, and if you want to use one that doesn’t actu­ally exists, then call bigAnimal::displayName(‘monkey’). You can even pass in your argu­ments if you want.

But maybe you guys are smarter than me. Can some­one explain what the ben­e­fit of this is sup­posed to be?

This entry was posted in Web Dev. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>