Actually when you call $this->load->model("book_model")
the CodeIgniter
does the job for you, which means CodeIgniter
's Loader class
has a method public function model(...)
which instantiate the model that you've passed as an argument, for example book_model
here.
Taken from model function
in Loader class
(located in system/core
)
if (in_array($name, $this->_ci_models, TRUE))
{
return;
}
It checks the _ci_models
protected array to see if the requested model is already loaded then it returns
and if it's not loaded then it loads it, i.e. (the last segment of model method)
$CI->$name = new $model(); // here $model is a variable which has the name of the requsted model
$this->_ci_models[] = $name; // stores the instantiated model name in the _ci_models[] array
return; // then returns
So, you don't need to use new
to instantiate it manually and once a model (same applies with other libraries or classes) is loaded then you can access/use it anywhere in your script.
Since CodeIgniter
uses the Singleton (an answer on SO about singleton pattern) design pattern so you have only one super global $CI
object (one instance of CodeIgniter) available and it carries everything you've loaded or you'll load.
To load the book_model
model and then call the initiateBook()
method of that model
$this->load->model("book_model"); // book_model loaded/instantiated
$this->book_model->initiateBook(); // initiateBook() function has been called