


First of all, let’s understand the above three keywords: this, self, parent, which is easier to understand literally, refers to this, self, father, haha, it’s more fun, let’s establish a few concepts first, Where are these three keywords used? Let’s briefly explain that this is a pointer to the current object (let’s use the pointer in C to look at it), self is a pointer to the current class, and parent is a pointer to the parent. Pointer to class. We frequently use pointers to describe here because there is no better language to express it. Haha, I didn’t learn Chinese well. -_-
# It’s not clear enough to say this, so let’s talk about it based on actual examples.
(1) this
The code is as follows:
<?php class UserName { //定义属性 private $name; //定义 构造函数 function construct( $name ) { $this->name = $name; //这里已经使用了this指针 } // 析构函数 function destruct(){} //打印用户名成员函数 function printName() { print( $this->name ); //又使用了this指针 } } //实例化对象 $nameObject = new UserName( "heiyeluren" ); //执行打印 $nameObject->printName(); //输出: heiyeluren //第二次实例化对象 $nameObject = new UserName( "PHP" ); //执行打印 $nameObject->printName(); //输出:PHP ?>
We see that the above class uses this pointer at line and line respectively, then this is pointing to who? In fact, this determines who it points to when instantiating it. For example, when the object is instantiated for the first time (line), then this points to $nameObject object, then when printing the line, Change print( $this->
(2)self
First of all, we must make it clear that self points to the class itself, that is, self does not point to any instantiated object. Generally, self is used to point to static variables in the class.
The code is as follows:
<?php class Counter { //定义属性,包括一个静态变量 private static $firstCount = ; private $lastCount; //构造函数 function construct() { $this->lastCount = ++selft::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域 运算符 号) } //打印最次数值 function printLastCount() { print( $this->lastCount ); } } //实例化对象 $countObject = new Counter(); $countObject->printLastCount(); //输出 ?>
We only need to pay attention to two places here, the first and second lines. We defined a static variable $firstCount in the second line, with an initial value of operator, then what we call at this time is the static variable $frestCount defined by the class itself. Our static variable has nothing to do with the instance of the following object, it is only related to the class, then if I call the class itself, then we cannot use this To reference, you can use self to reference, because self points to the class itself and has nothing to do with any object instance. In other words, if there are static members in our class, we must also use self to call them.
(3)parent
We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class.
The code is as follows:
<?php //基类 class Animal { //基类的属性 public $name; //名字 //基类的构造函数 public function construct( $name ) { $this->name = $name; } } //派生类 class Person extends Animal //Person类继承了Animal类 { public $personSex; //性别 public $personAge; //年龄 //继承类的构造函数 function construct( $personSex, $personAge ) { parent::construct( "heiyeluren" ); //使用parent调用了父类的构造函数 $this->personSex = $personSex; $this->personAge = $personAge; } function printPerson() { print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge ); } } //实例化Person对象 $personObject = new Person( "male", ""); //执行打印 $personObject->printPerson(); //输出:heiyeluren is male,this year ?>
We pay attention to these few details: member attributes are all public, especially those of the parent class, for inherited classes to access through this. We pay attention to the key point, line: parent::construct( "heiyeluren" ). At this time, we use parent to call the constructor of the parent class to initialize the parent class, because the members of the parent class are all public, so We can directly use this to call in the inherited class.
Summary:
This is a pointer to an object instance, self is a reference to the class itself, and parent is a reference to the parent class.
The above is the detailed content of Detailed explanation of the instance usage of the three keywords this, self and parent in php. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

报错的原因NameResolutionError(self.host,self,e)frome是由urllib3库中的异常类型,这个错误的原因是DNS解析失败,也就是说,试图解析的主机名或IP地址无法找到。这可能是由于输入的URL地址不正确,或者DNS服务器暂时不可用导致的。如何解决解决此错误的方法可能有以下几种:检查输入的URL地址是否正确,确保它是可访问的确保DNS服务器可用,您可以尝试在命令行中使用"ping"命令来测试DNS服务器是否可用尝试使用IP地址而不是主机名来访问网站如果是在代理

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

在介绍Python的self用法之前,先来介绍下Python中的类和实例我们知道,面向对象最重要的概念就是类(class)和实例(instance),类是抽象的模板,比如学生这个抽象的事物,可以用一个Student类来表示。而实例是根据类创建出来的一个个具体的“对象”,每一个对象都从类中继承有相同的方法,但各自的数据可能不同。1、以Student类为例,在Python中,定义类如下:classStudent(object):pass(Object)表示该类从哪个类继承下来的,Object类是所有


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version
