Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a function as an argument to another

Status
Not open for further replies.

Algernon83

Programmer
Jun 25, 2004
50
US
There's a point in my code where it's useful for a particular function to take another function as an argument. There's no trouble doing this with functions in the global scope, but I want to be able to pass it a method of a class (as in Class::Method). Unfortunately, it fails with a message of "undefined function: class::method". Is there a better fix for this than making a wrapper function, as I have it now?
 
Without seeing the relevant code (concise as possible), it's difficult to give you an answer beyond "Yes" or "No".

Lee
 
Here's a mock-up.

class myclass
{
function mymethod($foo)
{
echo $foo;
}
}

function callanotherfunction($f, $x)
{
return $f($x);
}

function wrapper($foo)
{
return myclass::mymethod($foo);
}

// Causes an error
//callanotherfunction("myclass::mymethod", "Hello, world!");

// Works
callanotherfunction("wrapper", "Hello, world!");
 
Have you tried:

callanotherfunction("myclass[red]->[/red]mymethod", "Hello, world!");

Lee
 
Actually, that's doesn't work even if you don't use it as a callback function, e.g.

myclass->mymethod("Hello, world!");

because myclass is a class, not an object of that class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top