1

If you are inside UsersController, what purpose does the $this->User part of the statement $this->User->find('all'); serve?

What is the $this referencing? Is it an instance of something? What is the current object in this case?

Chuck Burgess
  • 11,600
  • 5
  • 41
  • 74
BWelfel
  • 532
  • 6
  • 20
  • You really ought to just take the advice given in the post you made 15 minutes ago, and read the PHP documentation on objects. Marking exact dupe of http://stackoverflow.com/questions/999307/meaning-of-this-in-cakephp – Kenan Banks Jun 16 '09 at 02:56
  • I am asking specifically withing CakePHP in the instance of a UsersController. The $this is referring to something! – BWelfel Jun 16 '09 at 02:58
  • it is referring to the UsersController class... you shouldn't be trying to write CakePHP code without a basic understanding of object oriented programming syntax in PHP. – Paolo Bergantino Jun 16 '09 at 03:06
  • So you mean it is not an instance of that class. In Java for instance, when you use this, it refers to an instance of the class, not the class itself. – BWelfel Jun 16 '09 at 03:09
  • As pointed out here: http://stackoverflow.com/questions/151969/php-self-vs-this , shouldn't a reference to the class use self? – BWelfel Jun 16 '09 at 03:13

2 Answers2

2

The default behavior in Cake is to automatically associate a model with each controller. The convention is that the associated model will be the singular of the controller name so UsersController will automatically load the model named User, PeopleController will automatically load the model named Person. The models that are autoloaded like this then become available as attributes of your controller object.

You can choose which models are auto loaded for a controller by setting the $uses attribute of the controller object to an array containing the names of the models you want loaded although this is generally considered bad practice due to performance issues.

You can also set the $uses attribute to false if you do not wish to associate any model with your controller.

To load model instances at a later stage in your controller you can call $this->loadModel('Cow') and $this->Cow will now contain a reference to your Cow model.

jcoffey
  • 465
  • 4
  • 15
2

From the CakePHP Manual - Beginning with Cake

This manual assumes that you have a general understanding of PHP and a basic understanding of object-oriented programming (OOP).

There are thousands of sites on the web that will explain OOP and PHP to you. Here are two random ones.

Objected Oriented Programming and Object Oriented Programming with PHP

$this // is the current class
$this->User // is the calling the Model User 
Jack B Nimble
  • 5,039
  • 4
  • 40
  • 62