![]() Shoutbox (View Full Shoutbox)
|
||||
Click Refresh to load shouts. |
||||
|
[PHP]Dynamic function call
|
|
07-26-2009, 02:12 PM
Post: #1
|
|||
|
|||
|
[PHP]Dynamic function call
Hey,
is there a way to dynamically call a function without storing it's name in a temporary variable? I have function abc('abc','123') which returns the name of function to call. I'm interested only in following way of calling, cause the other methods don't handle references correctly. Code: $func();Thanks |
|||
|
07-26-2009, 03:23 PM
Post: #2
|
|||
|
|||
|
[PHP]Dynamic function call
I'm not sure but u can try this
Code: eval(); |
|||
|
07-26-2009, 03:41 PM
Post: #3
|
|||
|
|||
|
[PHP]Dynamic function call
Hey.
If you want to call the function dynamically and need to pass params by reference then you have to use this: call_user_func_array('func_name', array(&$my_param_ref)); instead of this: call_user_func('func_name', $my_param_ref); And calling functions directly by name accepts references as well. This code works: PHP Code: function add(&$a)Possible I didn't get your question, but it seems like you have everything you need to work with references in dynamic functions. Let me know if I've missed anything. Thanks. .:: Tibia 2 ::. |
|||
|
07-26-2009, 04:52 PM
Post: #4
|
|||
|
|||
|
[PHP]Dynamic function call
@Yabbo: yeah I know I can use eval but I prefer to use some built-in solution which is definitely faster.
@Stepler: Thanks, but that's not what I'm asking about. The point here is that I don't know what parameters are passed to the function and what the function is. All I have is function name and param string. This means I can't use any call_user_func~ because it requires me to specify how do I want to pass it. As for the $func($param) that's what I wrote in first post. The problem is that I can't store the function name in any additional variable and I need to operate on function result directly (I mean the function which returns function name) - it has to be a single expression. I need something like: funcWhichReturnsFunctionName('someParam')($paramToTheFunction, $anotherParam); |
|||
|
07-26-2009, 06:39 PM
(This post was last modified: 07-26-2009 06:57 PM by Stepler. Edit Reason: )
Post: #5
|
|||
|
|||
|
[PHP]Dynamic function call
Hey. I guess my English is too weak to understand that, I can't even get the point and have no idea what you are talking about. Nwm, this happens to me, hehe.
Update: After thinking a minute I have noticed that the string you've written: - funcWhichReturnsFunctionName('someParam')($paramTo TheFunction, $anotherParam); is almost equal to this: - call_user_func_array(funcWhichReturnsFunctionName('someParam'), array($paramToTheFunction, $anotherParam)); So this makes me confused even more. .:: Tibia 2 ::. |
|||
|
07-27-2009, 06:26 AM
(This post was last modified: 07-27-2009 06:29 AM by Snowak. Edit Reason: )
Post: #6
|
|||
|
|||
|
[PHP]Dynamic function call
It is similiar but makes large difference. The first method automatically detects which parameters have to be passed via reference. The second needs to be informed how should they be passed, by prefixing variable name with &. As I have no idea what function I am about to call and I only have the parameters functions such as preg_match~ will fail.
I'm writting a code obfuscator for personal use and the reason why I can't use temporary variables is that my parser replace all standard php function calls with obfuscated ones and they can be anywhere - in if statements, loops conditions etc... |
|||
|
07-27-2009, 11:31 AM
Post: #7
|
|||
|
|||
|
[PHP]Dynamic function call
Yea, I got it now. Well, I have never met such a need. The only idea I have in my head is about debug_backtrace(), it can get any parameters list and it returns references, not even sure if this works. Even though there is a small chance that it works I don't like it, It would be too strange
![]() Thanks for the explanation. .:: Tibia 2 ::. |
|||
|
07-27-2009, 12:50 PM
(This post was last modified: 07-27-2009 01:49 PM by Snowak. Edit Reason: )
Post: #8
|
|||
|
|||
|
[PHP]Dynamic function call
Are you sure debug_backtrace return references to all passed parameters? I was thinking about making a caller function which takes any number of parameters (using func_get_args()) but then I've noticed it returns copies... If I would be able to get references then things would get really simple.
/edit I've just coded a little test. Code: <?php So there are two things to note. First of all, the echo outputs 'a = 2'. The second is that var_dump shows that all variables in $t[x]['args'] are in fact references. Simple to guess, if I use trace(&$a) the above will work. So here's the point - is there any way to make a function with unknown number of parameters which are passed as references? Something like function(&$a, &$b, &$c...){...} God, I hope I won't end up with something like Code: function call($funcName, &$p1 = 'dummy', &$p2 = 'dummy', &$p3 = 'dummy', &$p4 = 'dummy', &$p5 = 'dummy', &$p6 = 'dummy', &$p7 = 'dummy', &$p8 = 'dummy', &$p9 = 'dummy', &$p10 = 'dummy')/edit Code: <?php Code: a = 3Hey, it's working! Lol... But I'd definitely prefer a clean solution
|
|||
|
07-27-2009, 01:58 PM
(This post was last modified: 07-27-2009 02:42 PM by Stepler. Edit Reason: )
Post: #9
|
|||
|
|||
|
[PHP]Dynamic function call
Hey.
After looking at your skyscraper... ![]() Even though I don't like it much, It isn't that uncommon to use this method. Usually we use this to access array with unknown amount of levels. I guess it's the best you can use. I don't know anything elegant to use as workaround. Let me know if you find. Here is a version of your code, looks a bit better. PHP Code: function call($funcName, &$p1=0,&$p2=0,&$p3=0,&$p4=0)UPDATE: Right, we have forgotten that only variables can be passed as references. The whole thing doesn't work if you do this: echo call('md5', 'test'); Thanks. .:: Tibia 2 ::. |
|||
|
07-27-2009, 04:10 PM
Post: #10
|
|||
|
|||
|
[PHP]Dynamic function call
Yea I know I could write it in more readible way but I've just coded it quickly to check whether it works
![]() I've just checked that return value of function can be passed as a reference so with a bit of luck the above should work, cause I am obfuscating all strings too, they are accessed by functions. I'll test it later, thanks for help
|
|||
|
« Next Oldest | Next Newest »
|

![[-]](images/mint/collapse.gif)





