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();
?>¢