Home > Backend Development > PHP Problem > What does $this mean in php

What does $this mean in php

青灯夜游
Release: 2023-03-13 16:16:02
Original
7599 people have browsed it

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);".

What does $this mean in php

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 -> 成员方法(参数列表);
Copy after login

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;
Copy after login

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 . &#39;<br>&#39;;
		$this -> url();
	}

	public function url() {
		echo &#39;https://www.php.cn/<br>&#39;;
		$this -> title();
	}

	public function title() {
		echo &#39;PHP入门教程<br>&#39;;
	}

}

$object = new Website(&#39;PHP中文网&#39;);
?>
Copy after login

Output results:

What does $this mean in php

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template