Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
帮助我理解$this是如何在PHP中使用的_Php_Oop_Variables_This - Fatal编程技术网

帮助我理解$this是如何在PHP中使用的

帮助我理解$this是如何在PHP中使用的,php,oop,variables,this,Php,Oop,Variables,This,简单地说,$this在PHP中是如何使用的 在JavaScript中,这是一个非常简单的概念,但在PHP中,由于某些原因,我无法理解这个变量及其函数。在任何一点上,它到底指的是什么?我在OOP方面只有三级经验,我想这就是为什么我很难理解它的用法,但我正在努力做得更好,我研究的很多代码都使用了这个变量。非常简单的英语: 一旦进入一个对象的函数,您就可以完全访问它的变量,但是要设置它们,您需要比只使用您想要使用的变量名更具体。要正确指定要使用局部变量,需要使用特殊的$this变量,PHP总是将该变量

简单地说,
$this
在PHP中是如何使用的

在JavaScript中,这是一个非常简单的概念,但在PHP中,由于某些原因,我无法理解这个变量及其函数。在任何一点上,它到底指的是什么?我在OOP方面只有三级经验,我想这就是为什么我很难理解它的用法,但我正在努力做得更好,我研究的很多代码都使用了这个变量。

非常简单的英语: 一旦进入一个对象的函数,您就可以完全访问它的变量,但是要设置它们,您需要比只使用您想要使用的变量名更具体。要正确指定要使用局部变量,需要使用特殊的
$this
变量,PHP总是将该变量设置为指向当前使用的对象

例如:

function bark()
{
    print "{$this->Name} says Woof!\n";
} 
无论何时在对象的函数中,PHP都会自动设置包含该对象的
$this
变量。您不需要做任何事情就可以访问它


普通英语:
$this
是一个伪变量,从对象上下文中调用方法时可用。它是对调用对象的引用(通常是该方法所属的对象,但如果该方法是从辅助对象的上下文静态调用的,则可能是另一个对象)

一个例子:

<?php
class A
{
    function foo()
    {
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

class B
{
    function bar()
    {
        // Note: the next line will issue a warning if E_STRICT is enabled.
        A::foo();
    }
}

$a = new A();
$a->foo();

// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
$b = new B();
$b->bar();

// Note: the next line will issue a warning if E_STRICT is enabled.
B::bar();
?>
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.

要详细说明shamittomar:

$这是一个句柄,可以引用在其中完成调用的当前对象。(所以它基本上指向它自己。假设我们有同一个类的多个对象,我们想在我们进行最终移动之前用(不同的)数据设置它:回显它。 当您不知道对象名称时,很难指向它自己

class SaySomething{
        private $the_line;// the variable exists only in this class!
        public function __construct($myline){
            $this->the_line = $myline;
           // see how it points to itself?
           // would there be a variable in the global scope then it would be not possible to "setup"
           // the object without it getting overwritten in the next setup.
        }

        //The function to echo the stuf. Can be callid by the "outside world"
        public function say_it(){
            echo $this->the_line;
            $this->add_exclamation();//call the function add_exclamation in this class/object.
        }

       //This function can not be called by the outside world because it's private.
       // The only way to call it is from inside this class. To point to this class and call the function:
       // $this->add_exclamation();
        private function add_exclamation(){
            echo "!";
        }

    }

    $obja = new SaySomething('my');
    $objb = new SaySomething('sample');
    $objc = new SaySomething('super');
    $objd = new SaySomething('text');

    //Mind: uptill nothing has been said, only the private variable $the_line has been set in the constructor.
    $obja->say_it();
    $objc->say_it();
    $objb->say_it();
    $objd->say_it();
要了解什么是类和什么是对象(它们往往会混淆很多…),请观看此幻灯片:

感谢您的深度和广度的回答。这是一个真正意义上的模范公民。