我正在为codeigniter创建一个新的库,看到我无法使用/加载我创建的帮助器

时间:2012-10-19 16:01:38

标签: php class codeigniter

基本上我有一个包含多个功能的帮助文件。 我在配置文件中自动加载帮助器,因此理论上不需要重新加载它。

但是,当我尝试在我正在处理的新库中使用我创建的函数(来自此帮助程序)时,它将通过此​​错误:

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

    }   

}   
}

2 个答案:

答案 0 :(得分:0)

下面有问题

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());

答案 1 :(得分:0)

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();

我希望有所帮助。