CodeIgniter Forums
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 {
    // properties
    var $CI;
var $encrypt_pass;
    var $redirect_to;
    var $_login_var = 'email';
    var $_password_var = 'password';
var $_permissions;

    
    function Auth()
    {
        $this->CI =& get_instance();
  $this->encrypt_pass = FALSE;
        $this->redirect_to = 'login';
        $this->hash_key = $this->CI->config->item('auth_hash');
    }

    
    function Authorize($permission_category = 1, $permission)
    {
       //here is a code to authorize users
     }

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

Filename: libraries/auth.php

Line Number: 31

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 Wink
If you don't define any default for an argument (i.e. there's nothing like
Code:
$argument = FALSE;
$argument = array();
$argument = '';
$argument = NULL;

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 Wink


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)
    {
     if (is_null($permission_category)) { $permission_category = 1; }

    }

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');
would work and use $permission_category of 1, as well as
Code:
$this->auth->Authorize('userlist', 4);
using $permission_category of 4

but if you
Code:
$this->auth->Authorize();
You'll get an error because the required first parameter, $permission, wasn't set.


Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012

[eluser]Adam_R[/eluser]
Cool. Even better logic. Thanks Smile


Passing variables to the function as parameters - throwing error - El Forum - 11-16-2012

[eluser]Aken[/eluser]
PHP docs are your best friend. Smile http://www.php.net/manual/en/functions.arguments.php