tags:

views:

257

answers:

4

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?

+3  A: 

It is an Object reference to the current object.

George Stocker
+1  A: 

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.

Triptych
A: 

Here's a good answer: link text

It also tells you the difference between $this and self

lyrae
+1  A: 

$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