Reputation: 173
What exactly means every part in string $this->load->model in codeigniter? In the code below "$this" makes reference to the User class(controller)? Also, does it invoke the method load that invokes model? Or does $this make reference to the name of every function where is put?
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->template->set_layout('adminLayout.php');
$this->load->model("User_model");
$this->load->Model('Auth_model');
}
function index()
{
$this->Auth_model->isLoggedIn();
$this->template->title('Admin ::');
$this->template->build('admin/index');
}
?>
Upvotes: 3
Views: 912
Reputation: 7111
$this
refers to global CodeIgniter object. If you var_dump($this)
in constructor or in called method, you will see all invoked and initialized code. You can track changes in way that you could load some library, helper, method, language, package or config or anything else allowed by Loader.php
class of framework. You will get similar output to outputting get_instance()
function.
That contstruction uses load()
method of Config.php core system file you could check with start on line 119. Model in line means type of file need to be loaded.
Basically it is refered to load method of Config class that act on needed Loader class methods (model, helper etc).
Upvotes: 1
Reputation: 339
That means you are loading models(User_model and Auth_model) so you can use the functions which are inside those models. For example: if you have Auth_model as follow
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function insert_valid_val($valid_data)
{
//Write queries here and return some values.
}
Then in controller you can invoke the insert_valid_val() as follow.
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->template->set_layout('adminLayout.php');
$this->load->model("User_model");
$this->load->Model('Auth_model');
}
function index()
{
$this->Auth_model->isLoggedIn();
$this->template->title('Admin ::');
$this->template->build('admin/index');
$returned_val = $this->Auth_model->insert_valid_val("send some array");
print_r($retunred_val);
}
?>
Upvotes: 0
Reputation: 6928
$this refers to the class you are in. $this isn't something from CodeIgniter, but from PHP. $this refers to the current object.
models are build usually to handle all database relations so basically the line
$this->load->model("User_model");
Says "Load the model "name of model" so we can use it.
Whenever you create a class
$something = new SomeClass();
Then $this
refers to the instance that is created from SomeClass, in this case $something. Whenever you are in the class itself, you can use $this to refer to this instance. So:
class SomeClass {
public $stuff = 'Some stuff';
public function doStuff()
{
$this->stuff;
}
}
Upvotes: 1