TechnoDiver Posted September 11, 2021 Share Posted September 11, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313721-optional-parameters/ Share on other sites More sharing options...
requinix Posted September 11, 2021 Share Posted September 11, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313721-optional-parameters/#findComment-1589824 Share on other sites More sharing options...
Strider64 Posted September 11, 2021 Share Posted September 11, 2021 Another thing is PHP OOP is great with inheritance where a lot of that code to put it nicely could be streamline and some of the methods wouldn't even have to be used. Something to look into? 1 Quote Link to comment https://forums.phpfreaks.com/topic/313721-optional-parameters/#findComment-1589830 Share on other sites More sharing options...
gizmola Posted September 12, 2021 Share Posted September 12, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313721-optional-parameters/#findComment-1589851 Share on other sites More sharing options...
maxxd Posted September 12, 2021 Share Posted September 12, 2021 Show the action() method please. I've said it before and I get the idea that I'm yelling at the storm, but you're making your life much more complicated and harder with the DB abstraction you are creating. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313721-optional-parameters/#findComment-1589852 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.