这篇文章主要介绍了PHP面向对象程序设计中的self、static、parent关键字用法,结合实例形式分析了self、static、parent关键字功能、应用场景及相关使用技巧,须要的朋友能够参考下,本文实例讲述了PHP面向对象程序设计中的self、static、parent关键字用法.分享给你们供你们参考,具体以下:
看到php里面有关于后期静态绑定的内容,虽然没有彻底看懂,可是也收获很多东西。
不存在继承的时候,不存在继承的意思就是,就书写一个单独的类来使用的时候。self和static在范围解析操做符 (::) 的使用上,并没有区别。
在静态函数中,self和static能够调用静态属性和静态函数(没有实例化类,所以不能调用非静态的属性和函数)。
在非静态函数中,self和static能够调用静态属性和静态函数以及非静态函数
此时,self和static的表现是同样的,能够替换为该类名的方式调用。php
<?php class Demo{ public static $static; public $Nostatic; public function __construct(){ self::$static = "static"; $this->Nostatic = "Nostatic"; } public static function get(){ return __CLASS__; } public function show(){ return "this is function show with ".$this->Nostatic; } public function test(){ echo Demo::$static."<br/>"; //使用类名调用静态属性 echo Demo::get()."<br/>"; //使用类名调用静态属性 echo Demo::show()."<br/>"; //使用类名调用静态属性 echo self::$static."<br/>"; //self调用静态属性 echo self::show()."<br/>"; //self调用非静态方法 echo self::get()."<br/>"; //self调用静态方法 echo static::$static."<br/>";//static调用静态属性 echo static::show()."<br/>";//static调用非静态方法 echo static::get()."<br/>"; //static调用静态方法 } } $obj = new Demo(); $obj->test();
输出结果;ide
static Demo this is function show with Nostatic static this is function show with Nostatic Demo static this is function show with Nostatic Demo
继承的时候
在继承时,self和static在范围解析操做符 (::) 的使用上有差异。parent也是在继承的时候使用的。函数
<?php class A{ static function getClassName(){ return "this is class A"; } static function testSelf(){ echo self::getClassName(); } static function testStatic(){ echo static::getClassName(); } } class B extends A{ static function getClassName(){ return "this is class B"; } } B::testSelf(); echo "<br/>"; B::testStatic();
输出结果:this
this is class A this is class B
self调用的静态方法或属性始终表示其在使用的时候的当前类(A)的方法或属性,能够替换为其类名,可是在类名很长或者有可能变化的状况下,使用self::的方式无疑是更好的选择。
static调用的静态方法或属性会在继承中被其子类重写覆盖,应该替换为对应的子类名(B)。
parent关键字用于调用父类的方法和属性。在静态方法中,能够调用父类的静态方法和属性;在非静态方法中,能够调用父类的方法和属性。设计
<?php class A{ public static $static; public $Nostatic; public function __construct(){ self::$static = "static"; $this->Nostatic = "Nostatic"; } public static function staticFun(){ return self::$static; } public function noStaticFun(){ return "this is function show with ".$this->Nostatic; } } class B extends A{ static function testS(){ echo parent::staticFun(); } function testNoS(){ echo parent::noStaticFun(); } } $obj = new B(); $obj->testS(); echo "<br/>"; $obj->testNoS();
输出结果;code
static this is function show with Nostatic
在文章的最后,咱们分析一个手册上的例子对象
<?php class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function who() { echo __CLASS__."\n"; } } class C extends B { public static function who() { echo __CLASS__."\n"; } } C::test(); ?>
输出结果继承
A C C
咱们单独拿出test方法进行分析:get
public static function test() { A::foo(); parent::foo(); self::foo(); }
1)A::foo();这个语句是能够在任何地方执行的,它表示使用A去调用静态方法foo()获得'A'。
2)parent::foo();C的parent是B,B的parent是A,回溯找到了A的foo方法;static::who();语句中的static::调用的方法会被子类覆盖,因此优先调用C的who()方法,若是C的who方法不存在会调用B的who方法,若是B的who方法不存在会调用A的who方法。因此,输出结果是'C'。[注1]
3)self::foo();这个self::是在B中使用的,因此self::等价于B::,可是B没有实现foo方法,B又继承自A,因此咱们实际上调用了A::foo()这个方法。foo方法使用了static::who()语句,致使咱们又调用了C的who函数。[注2]it
<?php class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function who() { echo __CLASS__."\n"; } } class C extends B { // public static function who() { // echo __CLASS__."\n"; // } } C::test(); ?>
输出结果:
A B B
注2:补充解释上面的(3)
<?php class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function foo() { echo "fooB"."\n"; } public static function who() { echo __CLASS__."\n"; } } class C extends B { public static function foo() { echo "fooC"."\n"; } public static function who() { echo __CLASS__."\n"; } } C::test(); ?>
输出结果:
A C fooB