代码之家  ›  专栏  ›  技术社区  ›  waiwai933

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

  •  87
  • waiwai933  · 技术社区  · 15 年前

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

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

    9 回复  |  直到 7 年前
        1
  •  111
  •   meder omuraliev    15 年前

    它是对当前对象的引用,在面向对象的代码中最常用。

    例子:

    <?php
    class Person {
        public $name;
    
        function __construct( $name ) {
            $this->name = $name;
        }
    };
    
    $jack = new Person('Jack');
    echo $jack->name;
    

    这将“jack”字符串存储为所创建对象的属性。

        2
  •  31
  •   Eric Leschinski Mr. Napik    10 年前

    了解 $this php中的变量是询问php是什么。不要问我们,问编译器:

    print gettype($this);            //object
    print get_object_vars($this);    //Array
    print is_array($this);           //false
    print is_object($this);          //true
    print_r($this);                  //dump of the objects inside it
    print count($this);              //true
    print get_class($this);          //YourProject\YourFile\YourClass
    print isset($this);              //true
    print get_parent_class($this);   //YourBundle\YourStuff\YourParentClass
    print gettype($this->container); //object
    
        3
  •  13
  •   ozahorulia Vivek Sadh    7 年前

    我知道它的老问题,不管怎样,另一个关于 $此 . $此 主要用于引用类的属性。

    例子:

    Class A
    {
       public $myname;    //this is a member variable of this class
    
    function callme() {
        $myname = 'function variable';
        $this->myname = 'Member variable';
        echo $myname;                  //prints function variable
        echo $this->myname;              //prints member variable
       }
    }
    

    输出:

    function variable
    
    member variable
    
        4
  •  8
  •   snicker    15 年前

    它是从类本身引用实例的方法,与许多其他面向对象的语言相同。

    PHP docs 以下内容:

    伪变量$This is available 当从中调用方法时, 对象上下文。$这是推荐信 到调用对象(通常是 方法所属的对象, 但可能是另一个物体,如果 方法是从 辅助对象的上下文)。

        5
  •  6
  •   Axel Guilmin Forrest Porter    9 年前

    让我们看看如果不使用$this会发生什么,并尝试使用实例变量和 具有以下代码段的同名构造函数参数

    <?php
    
    class Student {
        public $name;
    
        function __construct( $name ) {
            $name = $name;
        }
    };
    
    $tom = new Student('Tom');
    echo $tom->name;
    
    ?>
    

    它只回声

    <?php
    
    class Student {
        public $name;
    
        function __construct( $name ) {
            $this->name = $name; // Using 'this' to access the student's name
        }
    };
    
    $tom = new Student('Tom');
    echo $tom->name;
    
    ?>
    

    这就像是“汤姆”

        6
  •  2
  •   cp3    15 年前

    当您创建一个类时,您有(在许多情况下)实例变量和方法(aka)。函数)。$这将访问这些实例变量,这样您的函数就可以获取这些变量,并执行它们需要执行的操作。

    另一个版本的Meder示例:

    class Person {
    
        protected $name;  //can't be accessed from outside the class
    
        public function __construct($name) {
            $this->name = $name;
        }
    
        public function getName() {
            return $this->name;
        }
    }
    // this line creates an instance of the class Person setting "Jack" as $name.  
    // __construct() gets executed when you declare it within the class.
    $jack = new Person("Jack"); 
    
    echo $jack->getName();
    
    Output:
    
    Jack
    
        7
  •  2
  •   homi    9 年前

    $这是一个特殊变量,它引用同一个对象,即它本身。

    它实际上引用了当前类的实例

    下面是一个示例,将清除上面的语句

    <?php
     class Books {
      /* Member variables */
      var $price;
      var $title;
    
      /* Member functions */
      function setPrice($par){
         $this->price = $par;
      }
    
      function getPrice(){
         echo $this->price ."<br/>";
      }
    
      function setTitle($par){
         $this->title = $par;
      }
    
      function getTitle(){
         echo $this->title ." <br/>";
      }
    }
    ?> 
    
        8
  •  2
  •   miken32 Amit D    8 年前

    $this a reference to the calling object (通常是方法所属的对象,但如果从辅助对象的上下文静态调用该方法,则可能是另一个对象)。

        9
  •  0
  •   Marc W    15 年前

    它引用当前类的实例,如 米德尔 说。

    PHP Docs . 它在第一个示例中进行了解释。