Jump to content

Optional parameters


TechnoDiver

Recommended Posts

Good evening, Phreaks. Quick question. I have this method here ->

public function get($table, $where = [], $column = "*") {
        return $this->action("SELECT {$column}", $table, $where);
    }

It works great as long as the call to it includes the $where array. I want both $where and $column to be optional here and $where to be an array when it's included.

if I only use the $table parameter (which is the only one I want to be required). I keep getting a

Quote

Fatal error: Uncaught Error: Call to a member function count() on bool

Could one of you fine folks please explain to me about this error and why I need the $where parameter when I want it to be optional. Thank you

Link to comment
Share on other sites

Tip: if PHP is complaining about a specific line in a specific file where you are calling count() incorrectly then you should probably post that specific file with its specific line. As well as everything that's between it and this get() where you believe the problem is.

Link to comment
Share on other sites

As you know, you can have multiple optional parameters, but the problem with that is, as in your example, you want the where clause to be default, but the column parameter is something you want to pass, then you have to pass something for the where parameter as well.  You would need to pass the same default empty array to your get method:

 

$obj->get('atable', [], 'aCol');

 

There are lots of different ways to get around this issue.   One obvious and simple solution is to change to something like this:

public function get($table, $criteria = []) {
    if (!empty($criteria['where']) && is_array($criteria['where'])) {
        $where = $criteria['where'];
    } else {
        $where = [];
    }

    $column = empty($criteria['column']) ? '*' : $criteria['column'];

    return $this->action("SELECT {$column}", $table, $where);
}

 

Then you just call it with your array containing what you want:

 

$obj->get('yourtable', ['column' => 'foo']);

or 

$obj=>get('yourtable', ['where' => ['cola = 3', 'colb = "something"']);

 

This code is tightly bound, so another solution would be to have something like what you see with ORM's like Symfony Doctrine or Laravel Eloquent, where there is a query class, that can be used to define the elements of query in pieces, and then this can be passed to your class that actually constructs, executes and fetches your results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.