• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am creating a new library to codeignighter, seem i cannot use/load helper i've created

#1
[eluser]netameta1[/eluser]
basically i have a helper file with several function.
I auto load the helper in the config file so theoretically reloading it is not required.

However when i try to use the function that i created(from this helper) within a new library that i am working on it will through this error:

Code:
"Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/techwork/public_html/giverhub/application/libraries/Udetails.php on line 7"


Whenever i use that function anywhere else(module,controller,views) it works fine.

I then read and thought maybe i should try loading the helper after following instructions at:
http://ellislab.com/codeigniter/user-gui...aries.html

and try referencing and loading but that also through an error:

Code:
"Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/techwork/public_html/giverhub/application/libraries/Udetails.php on line 5"


Here is the library:
Code:
class Udetails{

$CI =& get_instance();// referencing as described in their website
$CI->load->helper('mylogin_helper');// loading the helper
public $member_session = is_member(); // using the function
public $username = $member_session['username'];
public $current_uID = $member_session['id'];
public $member_status = $member_session['status'];
    }

Here is the function within the helper:
Code:
if ( ! function_exists('is_member'))
    {
     function is_member(){
      $CI =& get_instance();
  $is_logged_in = $CI->session->userdata('is_logged_in');
  $username = $CI->session->userdata('username');
  $capabilities = $CI->session->userdata('capabilities');
  $user_id = $CI->session->userdata('id');
  switch ($capabilities){
   case 'registered':
     $level = 1;
     break;
   case 'confirmed':
     $level = 2;
     break;
   case 'charity_admin':
     $level = 3;
     break;
   case 'admin':
     $level = 4;
     break;
   case 'super_admin':
     $level = 5;
     break;
   default:
     $level = 0;
  }
  
  $userdetails = array();
  if(isset($is_logged_in) && $is_logged_in == true){
   if($level > 1){
    $userdetails['username'] = $username;
    $userdetails['status'] = TRUE;
    $userdetails['id'] = $user_id;
    return $userdetails;
   }
    
  }

}  
    }

Thanks in advanced.

#2
[eluser]PhilTem[/eluser]
You need to wrap this code

Code:
$CI =& get_instance();// referencing as described in their website
$CI->load->helper('mylogin_helper');// loading the helper
public $member_session = is_member(); // using the function
public $username = $member_session['username'];
public $current_uID = $member_session['id'];
public $member_status = $member_session['status'];

into the __construct()-method to make it work, but before that you should define the variables as class attributes, so you don't get referencing errors.

That's because you can't use anything but

Code:
private $var = array(), NULL, FALSE, '';

as initial value for a class attribute. And you can't put $CI =& get_instance() inbetween the class tags without wrapping a method around it.

#3
[eluser]netameta1[/eluser]
i changed it to this:
Code:
class Udetails{
public $member_session;
public $username;
public $current_uID;
public $member_status;
  
public function __construct(){
                public $member_session = is_member();
         public $username = $member_session['username'];
         public $current_uID = $member_session['id'];
         public $member_status = $member_session['status'];
}


is this the correct way ?

Also how will i get to those variables in other methods afterward ?

Thanks again

#4
[eluser]netameta1[/eluser]
Thanks for help everyone - found a solution.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.