Passing variables to the function as parameters - throwing error - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Passing variables to the function as parameters - throwing error (/showthread.php?tid=55919) |
Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]Adam_R[/eluser] I have an authorization library which looks like: Code: class Auth { When I will try to use Code: $this->auth->Authorize(); or Code: $this->auth->Authorize('userlist'); I'm getting error: Code: Message: Missing argument 2 for Auth::Authorize(), called in /controllers/admin.php on line 57 and defined Shouldn't in a first case use argument 1: $permission_category = 1 and an empty $permission as argument 2? Then in a second case argument 1 be $permission_category = 1 and argument 2 $permission = 'userlist' ? I'm little bit lost there. Thanks Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]PhilTem[/eluser] Nope, that's not how PHP is designed. PHP takes the arguments in order of appearance i.e. from left to right and not from right to left or any other ordering If you don't define any default for an argument (i.e. there's nothing like Code: $argument = FALSE; then it will be necessary to supply the argument. Hence you either want to make a default for the second argument or inverse the arguments ordering Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]Adam_R[/eluser] OK, so I have this to skip first argument: Code: function Authorize($permission_category = NULL, $permission = NULL) Is it good enough? Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]CroNiX[/eluser] Why do that when you can just assign 1 as the default to begin with? Then if nothing is passed in, it will be 1, or if something was passed in it will be that. Code: function Authorize($permission_category = 1, $permission = NULL) Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]Adam_R[/eluser] I had that way and it returned error when I have called for it as: Code: $this->auth->Authorize('userlist'); now I'm doing Code: $this->auth->Authorize(NULL,'userlist'); So if not passed first argument I'm getting 1 Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]CroNiX[/eluser] If $permission will be required, then have that be the first parameter and I wouldn't set a default value for it. Code: function Authorize($permission, $permission_category = 1) Then this Code: $this->auth->Authorize('userlist'); Code: $this->auth->Authorize('userlist', 4); but if you Code: $this->auth->Authorize(); Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]Adam_R[/eluser] Cool. Even better logic. Thanks Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012 [eluser]Aken[/eluser] PHP docs are your best friend. http://www.php.net/manual/en/functions.arguments.php |