Passing variables to the function as parameters - throwing error |
[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
[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
[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?
[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)
[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
[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();
[eluser]Aken[/eluser]
PHP docs are your best friend. http://www.php.net/manual/en/functions.arguments.php |
Welcome Guest, Not a member yet? Register Sign In |