php面向对象的简单总结 $this $parent self 虽然接触php比较长时间,但有时在使用一些基础东西的时候还会有些不确定,有些疑惑。面向对象涉及到的比较多,大概总结整理一下php的属性、对象,以及访问方式$this $parent self 的使用场景。
1. php类属性定义和访问方式:
1 tconst; //无错误,无输出
9 echo self::tconst; //正确输出 1
10
11 echo $this->tvar; // 注意tvar前不能有$符号
12 echo self::$tvar; // access to undeclared static property: testclass::$tvar
13 echo self::tvar; // tvar前需要加上$符号,undefined class constant 'tvar'
14
15 echo $this->tsta; //无错误,无输出
16 echo self::$tsta; //正确输出 3
17
18 }
19 }
20
21 $otestclass = new testclass();
总结几点:
在实例化对象时 $otestclass = new testclass(); 其中testclass()中的()可以省略,当构造函数有显式声明需要参数时,需要在这里传入参数
如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::
如果被引用的变量或者方法没有被声明成const或者static,那么就必须使用操作符->
从类的内部访问const或者static变量或者方法,使用自引用的self
从类的内部访问不为const或者static变量或者方法,使用自引用的$this
2. $this-> 究竟指向哪?
1 class testclass {
2 var $nick = '';
3
4 public function __construct($nick){
5 $this->nick = $nick;
6 }
7
8 public function display(){
9 echo $this->nick;
10 }
11 }
12
13 $otestclass1 = new testclass('frank');
14 $otestclass1->display(); //echo $otestclass1->nick; 和此结果相同
上面例子中,$this->display(); 其实就是 $otestclass1->nick,因此$this究竟指向哪是由所实例化的对象决定的,指向当前对象实例的指针。包括变量、方法都是如此
$this->方法() 的例子:
1 class baseclass{
2 public function testfunc(){
3 echo \n . 'i`m boss';
4 }
5 }
6
7 class parentclass extends baseclass{
8 public function testfunc(){
9 echo \n . '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 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 . \n;
12 echo $this->num . \n;
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是单独存在于各个具体实例中的。
5. 总结:
$this 指向当前的实例
$parent 是父类的引用
self 对当前类本身的一个引用
http://www.bkjia.com/phpjc/926456.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/926456.htmltecharticlephp面向对象的简单总结 $this $parent self 虽然接触php比较长时间,但有时在使用一些基础东西的时候还会有些不确定,有些疑惑。面向对象涉...