-1

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!

7
  • 3
    "Can I assume" -- can you just check it yourself? It would take couple of minutes Commented Mar 18, 2012 at 22:33
  • 2
    Thought it would also be a good reference for anyone in the future who was thinking the same thing. Commented Mar 18, 2012 at 22:34
  • 1
    if checking yourself takes less than asking a question - there is no reason to bother yourself and community, imho Commented Mar 18, 2012 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... Commented Mar 18, 2012 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. Commented Oct 12, 2012 at 20:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@zerkms figured it out before, and now future devs will be able to reference this post. Goal accomplished.
@Kovo: there are already a bunch of similar questions here. Goal has been accomplished long ago stackoverflow.com/questions/7213825/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.