欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-06-08 17:30:03
...
this,self,parent 区别 以及 PHP双冒号::的用法

PHP5是一具备了大部分面向对象语言的特性的语言,比PHP4有了很多的面向对象的特性,但是有部分概念也比较绕人,所以今天拿出来说说,说的不好,请高手见谅. (阅读本文,需要了解PHP5的面向对象的知识)
首 先我们来理解三个关键字: 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
//第二次实例化对象
$nameObject2 = new UserName( "PHP5" );
//执行打印
$nameObject2->printName(); //输出:PHP5
?>
我 们看,上面的类分别在11行和20行使用了this指针,那么当时this是指向谁呢?其实this是在实例化的时候来确定指向谁,比如第一次实例化对象 的时候(25行),那么当时this就是指向$nameObject对象,那么执行18行的打印的时候就把print( $this->name ),那么当然就输出了"heiyeluren"。第二个实例的时候,print( $this->name )变成了print( $nameObject2->name ),于是就输出了"PHP5"。所以说,this就是指向当前对象实例的指针,不指向任何其他对象或类。
?
?
(2)self
首先我们要明确一点,self是指向类本身,也就是self是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。
???? 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来调用父类的构造函数。
//基类
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:用变量在类定义外部访问
??? ??? 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:在类定义外部使用::
??? ??? 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方法
??? ??? 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:使用作用域限定符
??? ??????? 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:调用基类的方法
??? ????
??? 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

?

?

?

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • this,self,parent 差别 以及 PHP双冒号:的用法
  • 专题推荐