Viewed   78 times

I need to pass a function as a parameter to another function and then call the passed function from within the function...This is probably easier for me to explain in code..I basically want to do something like this:

function ($functionToBeCalled)

Is there a way to do that.. I am using PHP 4.3.9




I think you are looking for call_user_func.

An example from the PHP Manual:

function barber($type) {
    echo "You wanted a $type haircut, no problem";
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
Wednesday, December 21, 2022

To answer your question first: No, you can't (without resorting to evilCode) pass a function name as a parameter. But: What you want to archive is a poster-child-issue for an object oriented approach using inheritance.

You'd need a base-class:

class BaseClass
    function __construct($db) {
        $this->db = db;

and your implementations :

class MyClass extends BaseClass
    function __construct($db) {
        $this->userDAO = DAO_DBrecord::createUserDAO($this->db);

Just for the record: the evilCode would have been a) you could encapsulate your function in a create_function that can be used as an argument. b) you could pass the function name as a string to your function and then pass it to eval in the receiving function.

But remember: When eval or create_function looks like the answer you're probably asking the wrong questions!

See: related question

Thursday, September 15, 2022

My suggestion is use array instead of using number of argument, For example your function call should be like this.

$params[6] = 'value';

For identify that sixth parameter has set

function MyFunction($params){
 If ( isset($params[6]) ) // parameter six has value


I hope that it will be a alternate way

Friday, October 7, 2022

You need to do that yourself. You can use null to indicate that a default value should be used:

public function getSomething($orderBy = null, $direction = null, $limit = null) {
    // fallbacks
    if ($orderBy === null) $orderBy = 'x';
    if ($direction === null) $direction = 'DESC';

    // do something random

Then pass null when calling it to indicate that you want to use the defaults:

$random = $this->my_model->getSomething(null, null, 10);

Another possible solution that I use sometimes is an additional parameter at the very end of the parameter list, containing all optional parameters:

public function foo($options = array()) {
    // merge with defaults
    $options = array_merge(array(
        'orderBy'   => 'x',
        'direction' => 'DESC',
        'limit'     => null
    ), $options);

    // do stuff

That way you do not need to specify all optional arguments. array_merge() ensures that you are always dealing with a complete set of options. You would use it like this:

$random = $this->my_model->foo(array('limit' => 10));

It seems like there is no required parameter this particular case, but if you need one, simply add it in front of the optional ones:

public function foo($someRequiredParameter, $someOtherRequiredParameter, $options = array()) {
    // ...
Tuesday, October 4, 2022

Assuming the function is always part of the dyn object you can use notation like following to call a function:


So if you are able to adjust json you could send back something like:

"content":{ "mainObject": "dynoData" , "method" :"getRetailers"}

And translate it to your dynamic function using variables:


As an example using jQuery try using the following :


Which is the same as :

Monday, October 24, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :