Home php教程 php手册 PHP5中的this,self和parent关键字详解教程

PHP5中的this,self和parent关键字详解教程

Jun 13, 2016 pm 12:32 PM
parent php5 self th this Keywords and us Tutorial Detailed explanation first

首先我们来明白上面三个关键字: this,self,parent,从字面上比较好理解,是指这,自己,父亲,呵呵,比较好玩了,我们先建立几个概念,这三个关键字分别是用在什么地方呢?我们初步解释一下,this是指向当前对象的指针(我们姑且用C里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针。我们这里频繁使用指针来描述,是因为没有更好的语言来表达,呵呵,语文没学好。 -_-#
这么说还不能很了解,那我们就根据实际的例子结合来讲讲。
(1) this

复制代码 代码如下:


 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 
 ?>  


我们看,上面的类分别在行和行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象的时候(行),那么当时this就是指向$nameObject对象,那么执行行的打印的时候就把print( $this->name ),那么当然就输出了"heiyeluren"。第二个实例的时候,print( $this->name )变成了print( $nameObject->name ),于是就输出了"PHP"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。 
(2)self
首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。

复制代码 代码如下:


     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(); //输出  
 ?>  


我们这里只要注意两个地方,第行和第行。我们在第二行定义了一个静态变量$firstCount,并且初始值为,那么在行的时候调用了这个值得,使用的是self来调用,并且中间使用"::"来连接,就是我们所谓的域运算符,那么这时候我们调用的就是类自己定义的静态变量$frestCount,我们的静态变量与下面对象的实例无关,它只是跟类有关,那么我调用类本身的的,那么我们就无法使用this来引用,可以使用self来引用,因为self是指向类本身,与任何对象实例无关。换句话说,假如我们的类里面静态的成员,我们也必须使用self来调用。
(3)parent
我们知道parent是指向父类的指针,一般我们使用parent来调用父类的构造函数。

复制代码 代码如下:


 //基类 
 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  
 ?>  


我们注意这么几个细节:成员属性都是public的,特别是父类的,是为了供继承类通过this来访问。我们注意关键的地方,第行:parent::__construct( "heiyeluren" ),这时候我们就使用parent来调用父类的构造函数进行对父类的初始化,因为父类的成员都是public的,于是我们就能够在继承类中直接使用this来调用。
总结:
this是指向对象实例的一个指针,self是对类本身的一个引用,parent是对父类的引用。
基本上我所了解就这么多,肯定有理解错误之处,请高手指出!
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Tutorial on how to use Dewu

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in Oracle SQL

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

Tutorial on how to turn off the payment sound on WeChat

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

What software is photoshopcs5? -photoshopcs5 usage tutorial

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Mar 22, 2024 pm 12:21 PM

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to convert int type to string

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

In summer, you must try shooting a rainbow

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

Detailed explanation of the role and usage of PHP modulo operator

See all articles