EllisLab text mark
Advanced Search
     
Passing variables to the function as parameters - throwing error
Posted: 16 November 2012 12:52 PM   [ Ignore ]
Joined: 2011-03-17
28 posts

I have an authorization library which looks like:

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

$this->auth->Authorize(); 

or

$this->auth->Authorize('userlist'); 

I’m getting error:

MessageMissing 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

 

 
Posted: 16 November 2012 01:02 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-02-23
882 posts

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

$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

 Signature 

ignited Community Framework (WiP)  |  Read the User’s Guide. It won’t bite.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

CI example .htaccess

 
Posted: 16 November 2012 03:38 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-03-17
28 posts

OK, so I have this to skip first argument:

function Authorize($permission_category NULL$permission NULL
    
{
     
if (is_null($permission_category)) { $permission_category 1

    } 

Is it good enough?

 
Posted: 16 November 2012 04:04 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

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.

function Authorize($permission_category 1$permission NULL
 Signature 
 
Posted: 16 November 2012 04:07 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-03-17
28 posts

I had that way and it returned error when I have called for it as:

$this->auth->Authorize('userlist'); 

now I’m doing

$this->auth->Authorize(NULL,'userlist'); 

So if not passed first argument I’m getting 1

 
Posted: 16 November 2012 04:12 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3796 posts

If $permission will be required, then have that be the first parameter and I wouldn’t set a default value for it.

function Authorize($permission$permission_category 1

Then this

$this->auth->Authorize('userlist'); 

would work and use $permission_category of 1, as well as

$this->auth->Authorize('userlist'4); 

using $permission_category of 4

but if you

$this->auth->Authorize(); 

You’ll get an error because the required first parameter, $permission, wasn’t set.

 Signature 
 
Posted: 16 November 2012 04:13 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-03-17
28 posts

Cool. Even better logic. Thanks smile

 
Posted: 16 November 2012 08:27 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

PHP docs are your best friend. smile http://www.php.net/manual/en/functions.arguments.php