5

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?

3 Answers 3

7

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

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

1 Comment

This is what I used and the least obtrusive method provided, thanks. if(is_null($target)) $target = $this->_config->db();
2

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();
    }
}

2 Comments

But the OP want's to call a specific method by default rather than accept arbitrary callable's?
Well, you can just remove the first section of the elseif tree then. This version still does that, it just gives the extra option of passing a function.
1

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();
    }
}

1 Comment

Everywhere else I find "null" as default value and you used empty string. May I ask why? Is it because you want it to be in the same type (string) as the custom value might be? Any reason in PHP to do that? I'm just curious.

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.