`
aslijiasheng
  • 浏览: 57090 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

this,self,parent 区别 以及 PHP双冒号::的用法

    博客分类:
  • php
 
阅读更多

PHP5是一具备了大部分面向对象语言的特性的语言,比PHP4有了很多的面向对象的特性,但是有部分概念也比较绕人,所以今天拿出来说说,说的不好,请高手见谅. (阅读本文,需要了解PHP5的面向对象的知识)
首 先我们来理解三个关键字: this,self,parent,从字面上比较好理解,是指这,自己,父亲,呵呵,比较好玩了,我们先建立几个概念,这三个关键字分别是用在什么地方 呢?我们初步解释一下,this是指向当前对象的指针(我们姑且用C里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针。

这么说还不能很了解,那我们就根据实际的例子结合来讲讲。
(1) this
<?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
//第二次实例化对象
$nameObject2 = new UserName( "PHP5" );
//执行打印
$nameObject2->printName(); //输出:PHP5
?>
我 们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象 的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this-><name )变成了print( $nameObject->name ),那么当然就输出了"heiyeluren"。第二个实例的时候,print( $this->name )变成了print( $nameObject2->name ),于是就输出了"PHP5"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。
 
 
(2)self
首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。
<?php
     class Counter
     {
         //定义属性,包括一个静态变量
         private static $firstCount = 0;
         private $lastCount;
        //构造函数
         function __construct()
         {
              $this->lastCount = ++selft::$firstCount; //使用self来调用静态变量,使用self调用必须使用::(域运算符号)
         }
         //打印最次数值
         function printLastCount()
         {
               print( $this->lastCount );
         }
    }
//实例化对象
$countObject = new Counter();
$countObject->printLastCount(); //输出 1
?>

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

我 们注意这么几个细节:成员属性都是public的,特别是父类的,是为了供继承类通过this来访问。我们注意关键的地方,第25行:parent:: __construct( "heiyeluren" ),这时候我们就使用parent来调用父类的构造函数进行对父类的初始化,因为父类的成员都是public的,于是我们就能够在继承类中直接使用 this来调用。


总的来说 THIS 就是当前对象实例, static静态变量,const常量,构造函数,与对象的实例无关,它只是跟类有关,所以用::


Program List:用变量在类定义外部访问
    <?php
    class Fruit {
        const CONST_VALUE = 'Fruit Color';
    }
    
    $classname = 'Fruit';
    echo $classname::CONST_VALUE; // As of PHP 5.3.0
    
    echo Fruit::CONST_VALUE;
    ?>
Program List:在类定义外部使用::
    <?php
    class Fruit {
        const CONST_VALUE = 'Fruit Color';
    }
    
    class Apple extends Fruit
    {
        public static $color = 'Red';
    
        public static function doubleColon() {
            echo parent::CONST_VALUE . "\n";
            echo self::$color . "\n";
        }
    }
    
    Apple::doubleColon();
    ?>

程序运行结果:
1    Fruit Color Red
Program List:调用parent方法
    <?php
    class Fruit
    {
        protected function showColor() {
            echo "Fruit::showColor()\n";
       }
    }
    
    class Apple extends Fruit
    {
        // Override parent's definition
        public function showColor()
        {
           // But still call the parent function
            parent::showColor();
            echo "Apple::showColor()\n";
        }
    }
    
    $apple = new Apple();
   $apple->showColor();  不是静态 的
    ?>

程序运行结果:
1    Fruit::showColor()
2    Apple::showColor()
Program List:使用作用域限定符
    <?php
        class Apple
        {
            public function showColor()
            {
                return $this->color;
            }
        }
    
        class Banana
        {
            public $color;
    
            public function __construct()
            {
                $this->color = "Banana is yellow";
            }
    
            public function GetColor()
            {
                return Apple::showColor();///使用作用域限定符
            }
        }
    
        $banana = new Banana;
        echo $banana->GetColor();
    ?>

程序运行结果:
1    Banana is yellow
Program List:调用基类的方法
    <?php
    
    class Fruit
    {
        static function color()
       {
            return "color";
        }
    
        static function showColor()
        {
           echo "show " . self::color();
        }
    }
    
    class Apple extends Fruit
    {
        static function color()
        {
            return "red";
        }
    }
    
    Apple::showColor();
    // output is "show color"!
    
    ?>

程序运行结果:
1    show color

 

 

 

分享到:
评论

相关推荐

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

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

    探讨PHP中this,self,parent的区别详解

    {一}PHP中this,self,parent的区别之一this篇面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能。利用OOP的思想进行PHP的高级编程,对于提高PHP编程能力和规划web开发构架都是很有意义...

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

    PHP是一具备了大部分面向对象语言的特性的语言,比PHP有了很多的面向对象的特性,但是有部分概念也比较绕人,所以今天拿出来说说,说的不好,请高手见谅. (阅读本文,需要了解PHP的面向对象的知识)

    php class中self,parent,this的区别以及实例介绍

    2,this可以调用本类中的方法和属性,也可以调用父类中的可以调的方法和属性 二,self 1,self可以访问本类中的静态属性和静态方法,可以访问父类中的静态属性和静态方法。2,用self时,可以不用实例化的 三,...

    【JavaScript源代码】php类中static与self的使用区别浅析.docx

    php类中static与self的使用区别浅析  使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类: 使用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为...

    PHP中的self关键字详解

    前言 PHP群里有人询问self关键字的用法,答案是比较明显的:静态成员函数内不能用this调用非成员函数,但可以...self与parent的区分比较容易:parent引用父类/基类被隐盖的方法(或变量),self则引用自身方法(或变量

    PHP面向对象程序设计中的self、static、parent关键字用法分析

    本文实例讲述了PHP面向对象程序设计中的self、static、parent关键字用法.分享给大家供大家参考,具体如下: 看到php里面有关于后期静态绑定的内容,虽然没有完全看懂,但是也收获不少东西。 php官方手册介绍: ...

    PHP编程过程中需要了解的this,self,parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object Oriented Programming)现已经成为编程人员的一项基本技能。利用OOP的思想进行PHP的高级编程,对于提高PHP编程能力和规划web开发构架都是很有...

    self-driving cars : A survey

    无人车综述:Abstract— We survey research on self-driving cars published in the literature focusing on autonomous cars developed since the DARPA challenges, which are equipped with an autonomy system ...

    计算机后端-PHP视频教程. php之面向对象18 self和parent.wmv

    计算机后端-PHP视频教程. php之面向对象18 self和parent.wmv

    PHP 5.2 标准课程初级parent 与self 关键字

    PHP 5.2 标准课程初级parent 与self 关键字

    網路象棋程式設計

    一、 棋盤設計:在範例程式中,棋盤的設計,有兩個部分(使用的與被mark的),主要可以區分為使用winapi的畫線程式,與利用Tshape來畫線,最後筆者採用Tshape元件,可以避免棋盤重劃的問題。主要程式的計算方式為:1. ...

    基于iOS开发

    [self addObserver:self forKeyPath:@"isRecording" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil]; [self addObserver:self forKeyPath:@"isPlaying" options:...

    ios-网络请求上下拉加载数据.zip

    一 、使用方法设置 1、不需要下拉刷新也不需要上拉加载 [self setupEmpty:self.tableView]; [self setupRefresh:self.tableView option:ATRefreshNone]; [self headerRefreshing]; 2、下拉刷新 [self ...

    php 中self,this的区别和操作方法实例分析

    主要介绍了php 中self,this的区别和操作方法,结合实例形式分析了PHP面向对象程序设计中self,this的功能、区别、使用方法与操作注意事项,需要的朋友可以参考下

    PHP 5.2 标准课程初级parent 与 self 关键字-1

    PHP 5.2 标准课程初级parent 与 self 关键字_1

Global site tag (gtag.js) - Google Analytics