【问题标题】:I am creating a new library to codeignighter, seem i cannot use/load helper i've created我正在为 codeignighter 创建一个新库,似乎我无法使用/加载我创建的助手
【发布时间】:2012-10-19 16:01:38
【问题描述】:

基本上我有一个具有多个功能的帮助文件。 我在配置文件中自动加载助手,所以理论上不需要重新加载。

但是,当我尝试在我正在处理的新库中使用我创建的函数(从此帮助程序)时,将出现此错误:

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

每当我在其他任何地方(模块、控制器、视图)使用该功能时,它都可以正常工作。

然后我阅读并认为也许我应该按照以下说明尝试加载帮助程序: http://codeigniter.com/user_guide/general/creating_libraries.html

并尝试引用和加载,但这也是通过错误:

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

这里是图书馆:

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'];
}

这是帮助程序中的函数:

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;
        }

    }   

}   
}

【问题讨论】:

    标签: php class codeigniter


    【解决方案1】:

    下面一行有问题

    public $member_session = is_member();
    

    你不能initialize class variable with function call。它应该有一些静态值而不是动态

    因此,您还会在第 7 行遇到解析错误

    编辑

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

    在控制器中,

    $this->load->library('udetails');
    
    echo "<pre>";
     print_r ($this->udetails->get_data());
    

    【讨论】:

    • 我怎样才能给变量动态值呢?
    • 请使用__construct来初始化你的类变量
    • 所以基本上我只是在 __construct "public variable; 之前初始化变量;然后在 __construct 我做 $this->member_session = is_member(); ?然后我将如何能够访问它的值变量?
    • 所以基本上没有办法动态定义变量,以便我以后可以在同一类中的其他方法上使用它们?
    • 好吧,你的答案 GBD 部分正确我已经阅读了更多内容并找到了正确的方法。我会将答案发布为答案,但是我会选择您的答案作为正确答案,因为您提供了很多帮助。
    【解决方案2】:
    class Udetails{
    
        // initializing variables that will be used in other methods
    public $username;
    public $current_uID;
    public $member_status;
    
    public function __construct(){
        $member_session = is_member();
        $this->username = $member_session['username'];
        $this->current_uID = $member_session['id'];
        $this->member_status = $member_session['status'];
    }
    
    // All the gets
        public function anothermethod(){
             //$CI =& get_instance(); - need to load those on each method if needed
             //$CI->load->helper('mylogin_helper'); - need to load those on each method if needed
            //can now use any of the variable deined in the construct in other methods.
            return $this->username;
        }
    }
    

    在控制器中:

    $this->load->library('udetails');
    $somevar = $this->udetails->anothermethod();
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2021-05-31
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多