what is “$this” in php? [duplicate]

2020-07-26 12:56发布

Possible Duplicate:
What does the variable $this mean in PHP?

I know this is extremely basic. I am watching tutorials on YouTube about CakePHP and PHP and all of them keep using "$this", but none of them actually say what it is or what it is used for. I think it is a variable, but what does it contain, and why do all of the functions have to be run from it? Is it the current class? I read here that "$this" is different from self though, so I am not sure if this is the case.

标签: php cakephp this
3条回答
欢心
2楼-- · 2020-07-26 13:11

$this refers to the instance of the class (a.k.a. object). self is more or less the same, but for static classes.

查看更多
我只想做你的唯一
3楼-- · 2020-07-26 13:15

I suggest you to read http://php.net/oop in particular this section http://www.php.net/manual/en/language.oop5.basic.php

The 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 possibly another object, if the method is called statically from the context of a secondary object).

查看更多
ら.Afraid
4楼-- · 2020-07-26 13:26

if used in a class, $this refers to the object that it's in.

查看更多
登录 后发表回答