Home  >  Article  >  Backend Development  >  PHP Object-Oriented (OOP) Programming: Special Usage of "$this"_PHP Tutorial

PHP Object-Oriented (OOP) Programming: Special Usage of "$this"_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:59777browse

PHP Object-Oriented (OOP) Programming: Special usage of "$this"

Now we know how to access members in an object through "Object->Members" This is a form of accessing members of the object from outside the object. So if I want to let the method in the object access the properties of this object inside the object, or the method in the object calls the object's Other methods What should we do at this time? Because all members in the object must be called using the object, including calls between internal members of the object, PHP provides me with a reference to this object, $this, and each object has a reference to the object. $this represents this object and completes the call to the internal members of the object. The original meaning of this is "this". In the above example, we instantiate three instance objects $P1, $P2, and $P3. There is one $this each representing objects $p1, $p2, and $p3.

PHP Object-Oriented (OOP) Programming: Special Usage of $this_PHP Tutorial

We can see from the above figure that $this is the reference that represents the object inside the object. The same method is used to call members of this object inside the object and call members of the object outside the object.

$this->Attributes: $this->name; $this->age; $this->sex;

$this->Method: $this->say(); $this->run();

Modify the above example so that everyone states their name, gender and age:

<?php
class Person
{
	//下面是人的成员属性
	var $name; //人的名子
	var $sex; //人的性别
	var $age; //人的年龄

	//下面是人的成员方法
	function say() { //这个人可以说话的方法
		echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age;
	}

	function run() { //这个人可以走路的方法
		echo "这个人在走路";
	}
}

$p1 = new Person(); //创建实例对象$p1
$p2 = new Person(); //创建实例对象$p2
$p3 = new Person(); //创建实例对象$p3

//下面三行是给$p1对象属性赋值
$p1->name = "张三";
$p1->sex = "男";
$p1->age = 20;

//下面访问$p1对象中的说话方法
$p1->say();

//下面三行是给$p2对象属性赋值
$p2->name = "李四";
$p2->sex = "女";
$p2->age = 30;

//下面访问$p2对象中的说话方法
$p2->say();

//下面三行是给$p3对象属性赋值
$p3->name = "王五";
$p3->sex = "男";
$p3->age = 40;

//下面访问$p3对象中的说话方法
$p3->say();
?>

The output result is:

My name is: Zhang San Gender: Male My age is: 20 My name is: Li Si Gender: Female My age is: 30 My name is: Wang Wu Gender: Male My Age is: 40

Analyze this method:

function say() { //这个人可以说话的方法
	echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age;
}

There is a say() method in the three objects $p1, $p2 and $p3. $this represents these three objects respectively. Call the corresponding attributes and print out the value of the attributes. This is inside the object. As for accessing object properties, if you call the run() method in the say() method, you can use $this->run() in the say() method to complete the call.

Articles you may be interested in

  • Recommended PHP object-oriented introductory tutorials
  • PHP object-oriented rules
  • PHP object to array (Object to array) Array), Json to array (Json to Array) method
  • php uses curl to implement multi-threaded classes, php curl multi-thread download pictures
  • Classic dialogue between programmers and testers . These are summarized and shared by foreign programmers, saying they are universally applicable?
  • Analysis of issues related to abstract classes and abstract methods in php
  • PHP’s method of implementing gzip page compression
  • php determines whether a string is in English or in pure Chinese. How to combine Chinese and English

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912302.htmlTechArticlePHP Object-Oriented (OOP) Programming: Special uses of "$this" Now we know how to access the Members are accessed through the "object-member" method, which is outside the object...
Statement:
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