" to complete access to internal members of the object; the syntax "$ this -> member property;" or "$this -> member method (parameter list);"."/> " to complete access to internal members of the object; the syntax "$ this -> member property;" or "$this -> member method (parameter list);".">
Home >Backend Development >PHP Problem >What does $this mean in php
In PHP, "$this" means "current object". It is a pointer to the current object instance. It is used in conjunction with the connector "->" and is specially used to complete the internal members of the object. Access between; syntax "$this -> member attribute;" or "$this -> member method (parameter list);".
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer
$this means after instantiation The specific object is the current object; $this is a pointer to the current object instance and does not point to any other object or class.
In PHP object-oriented programming, once an object is created, there will be a special object reference "$this
" in each member method of the object. Which object the member method belongs to, "$this
" represents which object, and is used in conjunction with the connector ->
to complete access to internal members of the object. As shown below:
$this -> 成员属性; $this -> 成员方法(参数列表);
For example, there is a $name
attribute in the Website class. We can use the following method in the class to access the $name
member attribute:
$this -> name;
It should be noted that when using $this
to access a member attribute, it only needs to be followed by the name of the attribute, and the $
symbol is not required. In addition, $this
can only be used in objects, $this
cannot be used elsewhere, and things that do not belong to objects $this
cannot be called, so to speak. Without an object, there is no $this.
[Example] Use $this to call properties and methods in a class.
<?php header("Content-type:text/html;charset=utf-8"); class Website { public $name; public function __construct($name) { $this -> name = $name; $this -> name(); } public function name() { echo $this -> name . '<br>'; $this -> url(); } public function url() { echo 'https://www.php.cn/<br>'; $this -> title(); } public function title() { echo 'PHP入门教程<br>'; } } $object = new Website('PHP中文网'); ?>
Output results:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does $this mean in php. For more information, please follow other related articles on the PHP Chinese website!