PHP5指针的类别介绍
大家也许多首先我们来理解三个关键字: this,self,parent,从字面上比较好理解,是指这,自己,父亲,呵呵,比较好玩了,我们先建立几个概念,这三个关键字分别是用在什么地方 呢?我们初步解释一下,this是指向当前对象的指针(我们姑且用C里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针。
这么说还不能很了解,那我们就根据实际的例子结合来讲讲。
(1) PHP5指针之this
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"><span>class UserName </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//定义属性 </span></li> <li class=""><span>private $name; </span></li> <li class="alt"><span> </span></li> <li class=""><span>//定义构造函数 </span></li> <li class="alt"><span>function __construct( $name ) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name; //这里已经使用了this指针 </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//析构函数 </span></li> <li class="alt"><span>function __destruct(){} </span></li> <li class=""><span> </span></li> <li class="alt"><span>//打印用户名成员函数 </span></li> <li class=""><span>function printName() </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>print( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>name ); //又使用了this指针 </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//实例化对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">nameObject</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> UserName( "heiyeluren" ); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//执行打印 </span></li> <li class=""> <span>$nameObject-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printName(); //输出: heiyeluren </span> </li> <li class="alt"><span> </span></li> <li class=""><span>//第二次实例化对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">nameObject2</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> UserName( "PHP5" ); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//执行打印 </span></li> <li class=""> <span>$nameObject2-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printName(); //输出:PHP5 </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
我们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象 的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this->
(2)PHP5指针之self
首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"><span>class Counter </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//定义属性,包括一个静态变量 </span></li> <li class=""> <span>private static $</span><span class="attribute"><font color="#ff0000">firstCount</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li class="alt"><span>private $lastCount; </span></li> <li class=""><span>//构造函数 </span></li> <li class="alt"><span>function __construct() </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">lastCount</font></span><span> = ++selft::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号) </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>//打印最次数值 </span></li> <li class=""><span>function printLastCount() </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>print( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>lastCount ); </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>//实例化对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">countObject</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Counter(); </span> </li> <li class=""> <span>$countObject-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printLastCount(); //输出 1 </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
我们这里只要注意两个地方,第6行和第12行。我们在第二行定义了一个静态变量$firstCount,并且初始值为0,那么在12行的时候调用了这个值 得,使用的是self来调用,并且中间使用"::"来连接,就是我们所谓的域运算符,那么这时候我们调用的就是类自己定义的静态变量$ frestCount,我们的静态变量与下面对象的实例无关,它只是跟类有关,那么我调用类本身的的,那么我们就无法使用this来引用,可以使用 self来引用,因为self是指向类本身,与任何对象实例无关。换句话说,假如我们的类里面静态的成员,我们也必须使用self来调用。
(3)PHP5指针之parent
我们知道parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""><span>//基类 </span></li> <li class="alt"><span>class Animal </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>//基类的属性 </span></li> <li class=""><span>public $name; //名字 </span></li> <li class="alt"><span>//基类的构造函数 </span></li> <li class=""><span>public function __construct( $name ) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>//派生类 </span></li> <li class=""><span>class Person extends Animal //Person类继承了Animal类 </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>public $personSex; //性别 </span></li> <li class="alt"><span>public $personAge; //年龄 </span></li> <li class=""><span>//继承类的构造函数 </span></li> <li class="alt"><span>function __construct( $personSex, $personAge ) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>parent::__construct( "heiyeluren" ); //使用parent调用了父类的构造函数 </span></li> <li class=""> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">personSex</font></span><span> = $personSex; </span> </li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">personAge</font></span><span> = $personAge; </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>function printPerson() </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>print( $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>name. " is " .$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>personSex. ",this year " .$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>personAge ); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>//实例化Person对象 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">personObject</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Person( "male", "21"); </span> </li> <li class=""><span> </span></li> <li class="alt"><span>//执行打印 </span></li> <li class=""> <span>$personObject-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>printPerson(); //输出:heiyeluren is male,this year 21 </span> </li> <li class="alt"><span> </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

When a generic function handles pointer types in Go, it will receive a reference to the original variable, allowing the variable value to be modified. Reference types are copied when passed, making the function unable to modify the original variable value. Practical examples include using generic functions to compare strings or slices of numbers.

PyCharm is a powerful Python integrated development environment with rich functions and tools that can greatly improve development efficiency. Among them, the replacement function is one of the functions frequently used in the development process, which can help developers quickly modify the code and improve the code quality. This article will introduce PyCharm's replacement function in detail, combined with specific code examples, to help novices better master and use this function. Introduction to the replacement function PyCharm's replacement function can help developers quickly replace specified text in the code

Dogecoin is a cryptocurrency created based on Internet memes, with no fixed supply cap, fast transaction times, low transaction fees, and a large meme community. Uses include small transactions, tips, and charitable donations. However, its unlimited supply, market volatility, and status as a joke coin also bring risks and concerns. What is Dogecoin? Dogecoin is a cryptocurrency created based on internet memes and jokes. Origin and History: Dogecoin was created in December 2013 by two software engineers, Billy Markus and Jackson Palmer. Inspired by the then-popular "Doge" meme, a comical photo featuring a Shiba Inu with broken English. Features and Benefits: Unlimited Supply: Unlike other cryptocurrencies such as Bitcoin

The pointer type approach is available in Go language, which allows you to define a function of pointer type in order to modify the value pointed to without explicitly passing the pointer in the method signature. This provides code simplicity and efficiency since copy-by-value passes do not need to be copied. The syntax of pointer type method is: typeTypeName*Type\nfunc(t*TypeName)MethodName(). To use pointer type methods, you create a pointer to an instance of the type and then use that pointer to call the method. The benefits of pointer type methods include code simplicity, efficiency, and modifiability. It should be noted that the pointer type method can only be used for pointer types, and you need to be careful when using it, because the structure value pointed to may be accidentally

References and pointers in C++ are both methods of passing function parameters, but there are differences. A reference is an alias for a variable. Modifying the reference will modify the original variable, while the pointer stores the address of the variable. Modifying the pointer value will not modify the original variable. When choosing to use a reference or a pointer, you need to consider factors such as whether the original variable needs to be modified, whether a null value needs to be passed, and performance considerations.

The eighth color is a weapon in Neon Abyss. Many players want to know about the ballistics of the eighth color of the weapon and how to play with the weapon strength. So let’s take a look at the detailed guide to Neon Abyss’ eighth color weapon trajectory, weapon strength, and weapon gameplay. Neon Abyss Color 8 Detailed Guide Weapon Introduction: The Wizard’s Secret Weapon! Weapon attack speed: Normal Weapon strength: Moderate Weapon gameplay: The attack method of the eighth color is three single-target attacks and then launches a ray. Ballistic display:

A fast score query tool provides students and parents with more convenience. With the development of the Internet, more and more educational institutions and schools have begun to provide online score check services. To allow you to easily keep track of your child's academic progress, this article will introduce several commonly used online score checking platforms. 1. Convenience - Parents can check their children's test scores anytime and anywhere through the online score checking platform. Parents can conveniently check their children's test scores at any time by logging in to the corresponding online score checking platform on a computer or mobile phone. As long as there is an Internet connection, whether at work or when going out, parents can keep abreast of their children's learning status and provide targeted guidance and help to their children. 2. Multiple functions - in addition to score query, it also provides information such as course schedules and exam arrangements. Many online searches are available.
