There is no simple method of overloading constructors in PHP. There are few workarounds to achieve this. I use the following technique.
class UserIdentity extends CUserIdentity
{
public function __construct()
{
$arg_list = func_get_args();
switch(func_num_args())
{
case 1:
// calling from admin end....
$this->_user = $arg_list[0];;
parent::__construct($this->_user->username,$this->_user->password);
break;
case 2:
// calling from Login Model....
parent::__construct($arg_list[0],$arg_list[1]);
break;
}
}
// ........
}
Usage:
// passing two parameters to instantiate the class
$identity=new UserIdentity($this->username,$this->password);
$identity->authenticate();
// passing one parameter to instantiate the same class
$user = Users::model()->notsafe()->findByPk($id);
if(!empty($user)){
$identity2 = new UserIdentity($user);
$identity2->loginMember();
}
2 Comments
Great post! I just added it to my favorites.
Thanks Jon