Categories
Archives
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- July 2009
- June 2009
- May 2009
- March 2009
- February 2009
- January 2009
- August 2008
- July 2008
- June 2008
- November 2007
- October 2007
- July 2007
- June 2007
- April 2007
- January 2007
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
Tools




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?
Related Posts: