【发布时间】:2014-01-25 15:59:20
【问题描述】:
如何将一个 Model 类实例化为另一个在 codeigniter 中有参数的 Model 类。
账户类:
class Account extends CI_Model
{
$username;
$password;
public function __construct($username,$password);
{
parent::__construct();
$this->username=$username;
$this->password=$password;
}
function login()
{
$result=$this->db->query("SELECT username FROM account where username='$this->$username' and password='$this->password'");
return $result->row(1);
}
}
TestAccount 类:
class TestAccount extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->library('unit_test');
$this->load->model('account','',TRUE);
}
/* In this test, the valid values of the credentials are the following:
* Username: myUsername
* Password: myPassword
*/
function testCorrectCredentials()
{
$anAccount=new $account("myUsername","myPassword");
$result = $anAccount->login();
$test_res = ($result > 0); // matches content, the return value is the id
$expected_result = true;
$test_name = "Given the correct credentials(username and password exists in database), this function will be able to retrieve the league manager username";
$this->unit->run($test_res, $expected_result, $test_name);
}
}
我想实例化我的 Account 类,但是如何?
【问题讨论】:
-
可以用
$anAccount=new $account("myUsername","myPassword");代替$this->account->function_name("myUsername","myPassword") -
是的,我很欣赏你的回答,但是在 codeigniter 中实例化一个类是不可能的吗?因为我试图使它成为 OOP 方法。
-
这也是 OOPS 方法,但在 CI 方法中。
标签: php codeigniter