3

Possible Duplicate:
PHP: self vs. $this

What does $this-> mean in CakePHP?

Please answer this in two parts... What does $this refer to? What does -> refer to?

Can someone explain each part explicitly in terms of the statement $this->Post->find('all'); in the Post controller. Why do you need the ->Post part if it is in the Posts controller?

Community
  • 1
  • 1
BWelfel
  • 532
  • 6
  • 20
  • Here's a good answer: [link text](http://stackoverflow.com/questions/151969/php-self-vs-this) It also tells you the difference between $this and self – sqram Jun 16 '09 at 02:15

3 Answers3

6

$this refers to the class you want to use. for instance if you see $this->Post->find('all'), you're trying to access the class Post that extends AppModel. Through conventions, the Post Model uses the posts table in your database. $this->Post->find('all') is used because the AppModel has the find() method and the Post model extends AppModel.

http://api.cakephp.org/class/app-model http://book.cakephp.org/view/22/CakePHP-Conventions

donalg d
  • 194
  • 4
  • 11
4

It is an Object reference to the current object.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
1

You'll definitely want to read the PHP documentation on classes before diving into CakePHP

From the official reference:

Every class definition begins with the keyword class, followed by a class name, which can be any name that isn't a reserved word in PHP. Followed by a pair of curly braces, which contains the definition of the classes members and methods. A pseudo-variable, $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object).

Not the most facile of definitions, but this really is stuff you're gonna have to know to navigate the code in CakePHP.

Kenan Banks
  • 207,056
  • 34
  • 155
  • 173