php5 中$this ,self,parent,static const

关于php5 中$this ,self,parent,static const的使用,理清一下.const,$this,self static 运用与区别Code:

const,$this,self static 运用与区别

Code:

class foo{
 const TMP = 'This is const TMP';
 private static $tmp1 = 0;
 private $tmp2 = 1;
 function __construct()
 {
  print self::$tmp1;
  //print self::$tmp2; //error $tmp2 is not static
  //print $this->tmp1; //null $this->tmp1 is null,tmp1 is static
  print $this->tmp2;
  print self::TMP; //print 'this is const TMP'
 }
 
 static function test()
 {
  print "this is static fun";
 }
}

print foo::TMP; //print const TMP;
foo::test(); //print test() out;
$obj = new foo;
$obj->test(); //this is fun

parent 主要用在扩展class 上,

Code:

<?php
class foo{
 const TMP = 'This is const TMP';
 private static $tmp1 = 0;
 private $tmp2 = 1;
 function __construct()
 {
  print self::$tmp1;
  //print self::$tmp2; //error $tmp2 is not static
  //print $this->tmp1; //null $this->tmp1 is null,tmp1 is static
  print $this->tmp2;
  print self::TMP; //print 'this is const TMP'
 }
 
 static function test()
 {
  print "this is static fun";
 }
}

class foo2 extends foo{

 function __construct()
 {
  parent::__construct(); //__connstruct used
  parent::test(); //print static fun;
  print parent::TMP; //print const
  //print parent::tmp1; //error
 }
}
print foo::TMP; //print const TMP;
foo::test(); //print test() out;
$obj = new foo;
$obj->test(); //this is fun

print "/n Test parent /n";

$obj2 = new foo2();

?>¢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值