$this和self/parent的区别
$this-> 在类中访问实例化后的方法和属性
self::访问类本身中的静态方法和属性
parent::当前类为子类的时候 用来访问父类中的静态方法和属性
<?php
class Classy {
const STAT = 'S' ; // 常量
static $stat = 'Static' ; // 静态变量
public $publ = 'Public' ;
private $priv = 'Private' ;
protected $prot = 'Protected' ;
function __construct( ){ }
public function showMe( ){
print '<br> self::STAT: ' . self::STAT ;
print '<br> self::$stat: ' . self::$stat ; //static的成员,必须使用self来访问,使用 this会出错。
print '<br>$this->stat: ' . $this->stat ; // static的成员不能被$this访问,没有输出
print '<br>$this->publ: ' . $this->publ ; // 指向实例化后对象的变量
print '<br>' ;
}
}
$me = new Classy( ) ;
$me->showMe( ) ;

浙公网安备 33010602011771号