Hello, I have a problem.
When I create controller, I load model (AccountModel) to class variable "Model" and check if user is logged in:
function __construct()
{
parent::__construct();
$this->loadModel("AccountModel", "Model");
$account = $this->getUserAccount();
...
}
Fatal error occurs in getUserAccount():
Fatal error: Call to a member function getAccount() on a non-object in wwwroot/lib/account/account.php on line 57
That's line 57, I call getAccount()
on my previously loaded model:
$account = $this->Model->getAccount($_SESSION["account"]["user_account_id"]);
So, looks like $this->Model
is not object, but when I put var_dump($this->Model)
before calling getAccount()
, it says object(AccountModel)#26 (2) ...
I also did var_dump($this)
, which dumped controller and found that class variable $Model
exists and is instance of AccountModel.
Can anybody tell me what the hell is going on?
Btw, model is assigned like that (method of controller):
function loadModel($model_name, $var_name)
{
// blah blah blah
$obj = new $class_name();
$this->$var_name = $obj;
}
Update:
In __construct(), if I directly call getAccount() on my model (previously I called getUserAccount and then getAccount within) without parameter, PHP outputs error sayin:
Missing argument 1 for AccountModel::getAccount()
But when I add parameter it says it's not object again.
Solution:
When loading models, method loadModel() assigned model to instance of controller and stored every name of loaded model into staic property. An error occured when controller has been called second time, so second instance of controller didn't really get its model. Replaced staitc property with object property (checking if model has already been called avoids using require_once lowers overhead, but now will have to find a better solution for that part ;) Anyway, now it works, thanks everyone for helping.