CodeIgniter Forums
$this keyword in view in CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: $this keyword in view in CodeIgniter (/showthread.php?tid=74664)



$this keyword in view in CodeIgniter - Mansfield2565 - 10-22-2019

I'm trying to understand how 

Code:
$this->load->view()
 works [i]inside[/i] of a view file in CodeIgniter.
The core/Controller.php is calling core/Loader.php which then calls _ci_load() which in turn does an 

Code:
include('/path/to/view');
Shouldn't 

Code:
$this
 refer to the Loader class at that point? How is 

Code:
$this
 referring to the controller?
By my understanding, you should have to call $this->view() inside of a view file. Not $this->load->view() because the load() function is not accessible inside of the Loader. It's a class variable of the Controller base class. i.e, 

Code:
$this->load =& load_class('Loader');
Please note: I'm trying to understand the CodeIgniter internals. I know perfectly well how to embed view files in other view files as a CodeIgniter [i]user[/i]. Please do not leave answers explaining how to [i]use[/i] $this->load().


RE: $this keyword in view in CodeIgniter - InsiteFX - 10-22-2019

When it is used inside a view file $this-> is pointing to the views controller.

$this-> is the controller that fired off the view.


RE: $this keyword in view in CodeIgniter - dave friend - 10-22-2019

The reason $this is still the controller is that view files are not classes. Instead they are simply included into the current scope - usually the controller. In other words $this->load->view('some_view') is a fancy, high-powered way to say include(path/to/views/some_view.php)