News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Andrew Podner:
Overloading Create PHP Class Methods on the Fly
March 06, 2013 @ 11:51:57

Andrew Podner has a new post today looking at dynamic class method creation in PHP - aka "overloading" with the __call magic method.

What is overloading and what would I need it for? [...] In most languages, overloading just means you can have multiple methods with the same name, but they just had a different number/type of arguments. In PHP, it is a little different. Overloading in PHP means that you can actually create dynamic function names and the behavior will be dependent upon the function name that is used.

He gives an example through a sample application, first stating the requirements the business has for it then showing how to use the "__call" method to handle "getBy" requests made to a database class. It searches the database based on the field (ex. "getByusername" searches on "username") and he includes two examples of it in use. He also briefly touches on the use of the "__callStatic" magic method for handling static method calls similarly.

0 comments voice your opinion now!
method overloading magicmethod call callstatic getby


blog comments powered by Disqus

Similar Posts

Stefan Mischook's Blog: How to call functions from another class

DevShed: Positioning Strings with the show_xy() Method in PDF Documents with PHP 5

Gergely Hodicska's Blog: What is new in PHP 5.3 - Part 4

Content With Style: buggy behaviour of parent:: in PHP 5.3.3

Stuart Herbert's Blog: Quick Tip: Get, Set and Query in One Method


Community Events





Don't see your event here?
Let us know!


community opinion update mvc interview symfony tool language laravel security framework composer voicesoftheelephpant introduction series package version release podcast library

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework