tags:

views:

45

answers:

2

here's my getter:

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

here's my constructor

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

everytime i access this function:

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

i get this error

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
+1  A: 

Is encrypt defined as a public variable in the class? If not, the logic of your __get() function demands that it be read from $this->fields['encrypt'], which is never set, and is what's producing that error.

VoteyDisciple
let's say I have an encrypt class I want to call from this class. How do I do that? I have tried $this->encrypt = new Encrypt but I guess the class interprets it as one of ist attributes.
Let's be clear: the `Encrypt` *class* is irrelevant here. The problem is the `encrypt` (correctly lowercase) instance variable. You must declare that property as either `public $encrypt;` or `protected $encrypt;` (depending on what visibility you want it to have).
VoteyDisciple
$this->encrypt = new Encrypt() is perfectly valid. The encrypt property should be an instance of Encrypt.
Mike B
Of course it's perfectly valid; I've made no claim to the contrary. The property should still be declared as either `public` or `private`.
VoteyDisciple
A: 

Use get_object_vars on the object before attempting to access the encrypt property to see if it really exists. Something else could be going on.

var_dump(get_object_vars($myClass));

Edit:

After looking at your code. The get() method should never be called since it is only invoked when a referenced property is inaccessible. Are you declaring $encrypt as private? Are you declaring it at all? What makes you think get() should be invoked? (Formatter goes crazy when I try to put underscores infront of get() with a link).

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
  }
}
Mike B
will do...but first i'll try adding the encryt class instantiation in the __get magic method...hope it works