php面向对象的简单总结 $this $parent self

$this->方法() 的例子:

1 class baseClass{

2 public function testFunc(){

3 echo " " . 'I`m boss';

4 }

5 }

6

7 class parentClass extends baseClass{

8 public function testFunc(){

9 echo " " . 'I`m the up';

10 }

11 }

12

13 class testClass extends parentClass{

14 var $nick = '';

15

16 public function __construct($nick){

17 $this->nick = $nick;

18 }

19

20 public function display(){

21 echo $this->nick;

22 $this->testFunc();

23 }

24 }

25

26 $otestClass1 = new testClass('frank');

27 $otestClass1->display();

这样的代码最后的输出结果是什么呢?关键是看testFunc()方法。

如果在类中用$this调用一个当前类中不存在的方法或变量,它会依次去父类寻找,直到找不到再报错

基于第一条,如果找到了需要的方法或变量,就会停止寻找,如果其上级父类中还有同样的,则选择当前找到的

所以上面代码输出为:

frank

I`m the up

3. 关于parent::

parent是对父类的引用

1 <?php

2 class A {

3 public $a = 'aa';

4 public function callFunc(){

5 echo 'parent';

6 }

7 }

8

9 class B extends A{

10 public function __construct(){

11 parent::callFunc();

12 echo " " . $this->a;

13 }

14 }

15

16 $oB = new B;

比如,上面的代码中,在class B中,若要调用其父类A中的callFunc()方法,就需要使用parent::callFunc(),但A类中此方法必须是public修饰的,否则会提示 Fatal error: Call to private method A::callfunc() from context 'B'...

另外需要注意的是,对于父类中的属性$a,调用的时候用$this,用parent就访问不到了。若$a在A类中是这样定义的:public static $a,则访问的时候就需要parent::$a

注意,parent不具备传递性,它只能代表当前类的父类,若此例子A类继承base类,在base类中定义baseFunc()方法,那么在B类中使用parent::baseFunc()是错误的,只能在A类中这样使用。

4. self::又指向哪里?

简单的说,self和某具体实例没有关系,它只指向当前类,不会受某具体类对象的影响

1 class testClass{

2 public static $count = 0;

3 public $num = 0;

4

5 function __construct(){

6 self::$count++;

7 $this->num++;

8 }

9

10 function display(){

11 echo self::$count . " ";

12 echo $this->num . " ";

13 }

14 }

15

16 $oTest1 = new testClass;

17 $oTest1->display(); //输出: 1 1

18

19 $oTest2 = new testClass;

20 $oTest2->display(); //输出: 2 1

上面例子中self::$cout始终指向该类本身,而$num是单独存在于各个具体实例中的。

原文地址:https://www.cnblogs.com/2881064178dinfeng/p/7219192.html