【问题标题】:How to instantiate a Model class to another Model class that has a parameter in codeigniter如何将模型类实例化为另一个在 codeigniter 中具有参数的模型类
【发布时间】: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


【解决方案1】:

当您加载模型时,它所在的文件会自动包含在内,这意味着您不必仅通过 $account 来访问它,在您的情况下,您只需这样做

$anAccount = new Account("myUsername","myPassword");

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多