-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!

Kovo
  • 1,687
  • 14
  • 19
  • 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... – Lawrence Cherone Mar 18 '12 at 22:49
  • @LawrenceCherone Yup, this was just an example – Kovo Mar 18 '12 at 23:05
  • possible duplicate of [Use a variable to define a PHP function](http://stackoverflow.com/questions/7213825/use-a-variable-to-define-a-php-function). @Kovo That's sloppy logic. If that were true you should get to work duplicating the php.net manual as stackoverflow questions. – Mike B Mar 19 '12 at 01:45
  • 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](http://data.stackexchange.com/stackoverflow/queries). – Sampo Sarrala - codidact.org Oct 12 '12 at 20:26

1 Answers1

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • @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 http://stackoverflow.com/questions/7213825/use-a-variable-to-define-a-php-function – zerkms Mar 18 '12 at 23:10