在PHP中,变量$这意味着什么?

在PHP中,变量$这意味着什么?

我看到了变量$this一直在PHP中,我不知道它是用来做什么的。我从未亲自使用过它,搜索引擎也忽略了$最后我搜索了“这个词。

有人能告诉我$这个变量在PHP中是如何工作的吗?


jeck猫
浏览 547回答 3
3回答

陪伴而非守候

了解$thisphp中的变量是询问PHP是什么。不要问我们,问编译器:print gettype($this);            //objectprint get_object_vars($this);    //Arrayprint is_array($this);           //falseprint is_object($this);          //trueprint_r($this);                  //dump of the objects inside itprint count($this);              //trueprint get_class($this);          //YourProject\YourFile\YourClassprint isset($this);              //trueprint get_parent_class($this);   //YourBundle\YourStuff\YourParentClassprint gettype($this->container); //object

人到中年有点甜

我知道这是个老问题,不管怎么说,另一个确切的解释是$这个. $这个主要用于引用类的属性。例子:Class A{    public $myname;    //this is a member variable of this classfunction callme() {     $myname = 'function variable';     $this->myname = 'Member variable';     echo $myname;                  //prints function variable     echo $this->myname;              //prints member variable    }}产出:function variable member variable
打开App,查看更多内容
随时随地看视频慕课网APP