Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Let's pretend this is happening inside of a class method (pure example):

public function runEvent($funcName, $params)
{
 $funcName($this, $params);
}

//somewhere else
function myFunc($anBOject, $paramsHere, $somethingElse = NULL)
{
 //do stuff
}

$SomeClassObj->runEvent('myFunc', array('dog', 'cat'));

Can I assume PHP will execute myFunc with the first parameter being $this, second being $params, and then NULL as the 3rd param (by default)?

This question is more just for understanding how PHP deals with variable functions. Im not actually having any issues in a certain project.

Thanks!

share|improve this question
3  
"Can I assume" -- can you just check it yourself? It would take couple of minutes –  zerkms Mar 18 '12 at 22:33
2  
Thought it would also be a good reference for anyone in the future who was thinking the same thing. –  Kovo Mar 18 '12 at 22:34
1  
if checking yourself takes less than asking a question - there is no reason to bother yourself and community, imho –  zerkms Mar 18 '12 at 22:40
    
you should also make sure you check the function exists before calling it and protect the script from injected code else you could have problems later on... –  Loz Cherone Mar 18 '12 at 22:49
1  
@MikeB it seems that there is already almost every PHP manual page here at SO.... no need to duplicate, solution is to query the database. –  Sampo Sarrala Oct 12 '12 at 20:26
show 2 more comments

1 Answer

up vote 1 down vote accepted

Yes.

$funcName($this, $params);

is called exactly the same way as

myFunc($this, $params);

The first parameter is $this, the second $params, there's no third.

To call functions with a variable number of arguments, use call_user_func_array.

share|improve this answer
    
Perfect, thanks. –  Kovo Mar 18 '12 at 22:40
    
@zerkms figured it out before, and now future devs will be able to reference this post. Goal accomplished. –  Kovo Mar 18 '12 at 23:06
1  
@Kovo: there are already a bunch of similar questions here. Goal has been accomplished long ago stackoverflow.com/questions/7213825/… –  zerkms Mar 18 '12 at 23:10
    
@zerkms Cool story –  Kovo Mar 18 '12 at 23:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.