0

这是我的吸气剂:

public function __get($field)
    {
        if ($field == 'userId'):
            return $this->uid;
        else:
            return $this->fields[$field];
        endif;
    }

这是我的构造函数

public function __construct()
    {
        $this->uid = null;
        $this->fields = array(
            'username' => '',
            'password' => '',
            'firstname' => '',
            'lastname' => '',
            'email' => '',
            'is_active' => false
        );
            $this->session = Session::Instance();
            $this->profiler = new Profiler();
            $this->encrypt = new Encrypt();
    }

每次我访问此功能时:

private function handle_pass($password, $username=null, $encrypt=true)
    {
        if ($encrypt) :
            return $this->encrypt->encode($password);
        else:
            $query = ORM::factory('user');
            $result = $query
                ->select('password')
                ->where('username', $username)
                ->find();
            $compare_pass = $this->encrypt->decode($password);
            return ($compare_pass === $result->password);
        endif;
    }

我得到这个错误

application/libraries/User.php: Undefined index: encrypt // this is the error message
application/libraries/User.php:User->__get( encrypt ) // this is the last method executed
4

2 回答 2

1

encrypt定义为public类中的变量吗?如果不是,则__get()函数的逻辑要求从 读取它$this->fields['encrypt'],它永远不会设置,并且是产生该错误的原因。

于 2009-08-20T01:11:50.617 回答
0

在尝试访问 encrypt 属性之前在对象上使用get_object_vars以查看它是否真的存在。其他事情可能正在发生。

var_dump(get_object_vars($myClass));

编辑:

看了你的代码后。永远不应调用get()方法,因为它仅在引用的属性不可访问时才被调用。您是否将 $encrypt 声明为私有?你要宣布它吗?是什么让您认为应该调用get() ?(当我尝试用链接在 get() 前面加上下划线时,格式化程序会发疯)。

class myClass()
{
  public $encrypt;

  public function __construct() 
  {
    $this->encrypt = new Encrypt();
  }

  public function __get($property)
  {
    return $this->$property;
  }

  public function handle_pass()
  {
    $this->encrypt->decode(); 
    // Since $encrypt is public, __get() will not be invoked
  }
}
于 2009-08-20T01:14:18.287 回答