![]() |
My Favourite PHP Magic Method: __call
Since the introduction of PHP5, us programmers have been treated to an abundance of magic methods. These are used in classes and provide extra functionality. They also tend to save a lot of time.
I'm here to introduce my favourite magic method, __call(). I use this predominantly within my database files (models in an MVC framework). It allows me to create the functions below without having to write them out individually as they all bring back effectively the same data.
The items highlighted in bold are the crucial parts of the string. The parts that are not bold are just there to give the function name some meaning. Let's take my member class as a prime example. You often find yourself wanting to get just one column. I could write the functions out individually like so:
The above functions would, in essence, return to me an email from the first function, and the birth date from the second. However, to save on the fingers it'd be great to bundle those into one function - __call()! We can compile the __call() function like so to accept our getSomethingBySomething() function and extract out the elements we need to query the database. PHP Code:
...And that's it! Your query is ready to be compiled like so: PHP Code:
|
Nice contribution Wildhoney. The magic functions are a great tool to have in your PHP development toolbox.
|
Wow, powerful.
|
The magic functions are extremely powerful and can be used to create some very impresive frameworks! Nice contribution!
|
Wow thats really cool :) I never thought of using it like that! At work we're trapped at PHP4 as we need to support as many server setups as possible, so I haven't delved into much of the php5 goodness.
|
It's a fantastic release. Adds far too much to even consider remembering it all. PHP4 is very limited on the OOP side of things - but PHP5. Wow.
|
PHP Code:
Cause what if you have several tables? :S getUsernameByWarnings(5) Ofcourse I could store the warnings in a column in the username table, but what if I don't(the warnings was just an example, I'm sure you get the point)?? How would I then be able to search the right table, by using this function? |
Well, this was based on an MVC pattern and so that function would be either cloned for every class that represents a table in the database, and so you would change the static table name manually. Or as an alternative, you could make the class central to everything and change the dynamic table name depending on which class you're calling it from.
|
Yea, but let's say I have a membersclass that deals with all the members.
But I have several tables concerning the members. Users Warnings Logs For instance. How would I then base that function on what class, since I'm in the membersclass, but I have several tables concerning the members? :// |
Actually I thought of something, the $aArgs is an array?
So if I do like: PHP Code:
PHP Code:
EDIT: And that would get every user, who has 5 warnings, from the warnings table. |
PHP5 Magic Tutorials
I have developed a complete set of tutorials on PHP5 Magic Methods, you can read it here:
http://www.sunilb.com/category/php/php5-magic-methods Hope this helps. Also if you have any questions, please post them here or leave comments my blog. |
| All times are GMT. The time now is 08:43 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0