Neden bir göstergesi olarak bir sınıf örneğini kullanarak benim alıcı sihirli bir yöntemdir?

2 Cevap php

burada benim alıcı bulunuyor:

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

burada benim kurucu bulunuyor

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

her i bu işleve erişebilirsiniz:

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

ben bu hatayı alıyorum

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

2 Cevap

encrypt sınıfında a public değişken olarak tanımlanmış mı? Değilse, __get() fonksiyonu mantığı o $this->fields['encrypt'], asla set edildiği okunmasını talep, ve bu hatayı üreten budur.

Gerçekten olup olmadığını görmek için şifrelemek özelliğine erişmek için denemeden önce nesne üzerinde get_object_vars kullanın. Başka bir şey oluyor olabilir.

var_dump(get_object_vars($myClass));

Edit:

Sizin kod baktıktan sonra. Başvurulan bir özellik erişilemez olduğunda yalnızca çağrılan beri get() çağrılacak yöntemi asla. Eğer $ şifrelemek özel olarak ilan edilir? Eğer her bildirerek mı? Ne get() çağrılması gerektiğini düşünüyorum yapar? (I () bir link ile get içimde çizgi koymak çalıştığınızda Formatör deli gider).

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