0

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

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

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

    }   

}   
}
4

2 回答 2

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());
于 2012-10-19T16:05:10.647 回答
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();

我希望这会有所帮助。

于 2012-10-19T17:19:03.627 回答