![]() |
When PHP tries to imitate C
Hi !
I have been impressed by this topic where Wildhoney gives a fantastic use of the __call magic method and it gave me the idea to propose mine. Don't hesitate to criticize :) If someone here knows the C development, you have certainly heard about va_list. it's this kind of functions which can take as many parameters as you want. For instance, in C, the first function you learn (printf) is a va_list. Indeed, you can give to it a string and after, as many parameters you have defined in the first string. The prototype of a such function is : "int printf (const char *format, ...);" The goal is to do the same in PHP with __call For instance, an easy example, you want to do this kind of operation : PHP Code:
I propose this (in a class "Operation") PHP Code:
I confess I haven't tested it yet. Like we say in french, "à vue de nez, c'est bon" :D What do you think of the concept ? I ask myself if the regulars accesses to the private methods don't burden the code in ram. |
func_get_args(); would also solve this problem and looks a lot less messy. (My first post by the way! :) )
|
Indeed, thank you, I didn't know this function :)
|
It's an interesting class. Incidentally, welcome to the community Sjag! However,
func_get_args wouldn't cut it here because I think Gibou intended it to be so he can call the function for what it is. Instead of:php Code:
You could do: php Code:
I've come with a much more condensed function - still using the unsurpassed power of the magic method, __call! Here it is - based on Gibou's function:php Code:
You can then use it like so: php Code:
Which would add 10 to 7, 5 to 17, and 2 to 22, totalling 24. You can also do more complex things such as: php Code:
Which multiplies 10 by 5, to give us 50, then 50 by 2 to give us 100! I'm quite proud of this as I've never been any good at maths :( ! |
another interesting topic containing __call().
Welcome to talkphp guys. |
Wildhoney > Indeed, that was the aim of this method : a clear way to do some similar operations.
sketchMedia > Thank you for the welcome. I have already read this topic but I think it won't be useless for me to re-read it. |
| All times are GMT. The time now is 01:09 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0