Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function as parameter default

Tags:

function

oop

php

Take the following function for example:

private function connect($method, $target = $this->_config->db()) {
    try {
        if (!($this->_pointer = @fopen($target, $method)))
            throw new Exception("Unable to connect to database");
    }  catch (Exception $e) {
            echo $e->getMessage();
    }
}

As you can see I inserted the function $this->_config->db() into the parameter $target as it's default value. I understand this is not the correct syntax and am just trying to explain my aim.

$this->_config->db() is a getter function.

Now I know I can use an anonymous function and call it via $target later, but I want $target to also accept direct string values.

How could I give it a default value of the whatever is returned by $this->_config->db() and still be able to overwrite it with a string value?

like image 269
George Reith Avatar asked Jan 05 '12 10:01

George Reith


People also ask

What is default function parameter in PHP?

The default parameter concept comes from C++ style default argument values, same as in PHP you can provide default parameters so that when a parameter is not passed to the function. Then it is still available within the function with a pre-defined value. This function also can be called optional parameter.

Can we pass function as a parameter in PHP?

PHP Parameterized functions are the functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name.

Is it possible to give a default value to a function parameter?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

How do you set a parameter to default value?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.


3 Answers

Why not accept NULL values by default (test with is_null()) and if so call your default function?

like image 130
user268396 Avatar answered Nov 02 '22 23:11

user268396


You can use is_callable() and is_string().

private function connect($method, $target = NULL) {
    if (is_callable($target)) {
        // We were passed a function
        $stringToUse = $target();
    } else if (is_string($target)) {
        // We were passed a string
        $stringToUse = $target;
    } else if ($target === NULL) {
        // We were passed nothing
        $stringToUse = $this->_config->db();
    } else {
        // We were passed something that cannot be used
        echo "Invalid database target argument";
        return;
    }
    try {
        if (!($this->_pointer = @fopen($stringToUse, $method)))
            throw new Exception("Unable to connect to database");
    }  catch (Exception $e) {
            echo $e->getMessage();
    }
}
like image 34
DaveRandom Avatar answered Nov 03 '22 01:11

DaveRandom


I would perform a check to see if a value was passed and call my function in a simple check inside the method:

private function connect($method, $target = '') {
    try {
        if ($target === '') {
            $target = $this->_config->db()
        }

        if (!($this->_pointer = @fopen($target, $method))) {
            throw new Exception("Unable to connect to database");
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
like image 22
Fenton Avatar answered Nov 03 '22 00:11

Fenton