I know that in Object Oriented Programming, $this refers to the current object. But in the code of the ArticlesController Class below,
public function index()
{
$articles = $this->Articles->find('all');
$this->set(compact('articles'));
}
'$this' seems to be referring to the class I want to use, which is the Articles class. I am very confused with what is going on. Can $this be used in this way too?
'$this' seems to be referring to the class I want to use, which is the Articles class. Can $this be used in this way too?
$this
does not referer to the ArticlesTable
class, but $this->Articles
does.
Your ArticlesController
class has a property called Articles
which is an instance of the ArticlesTable
class.
Heres an example of how it might be done
class ArticlesController
{
private $Articles;
public function __construct()
{
$this->Articles = new ArticlesTable();
}
public function index()
{
$articles = $this->Articles->find('all');
}
}
class ArticlesTable
{
public function find()
{
echo "find method called";
}
}
$controller = new ArticlesController();
$controller->index();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.