class self_test {
static $instance;
public function __construct(){
self::$instance = 'instance';//静态属性只能通过self来访问
}
public function tank(){
return self::$instance;//访问静态属性
}
}
$str = new self_test();
echo $str->tank();
页面输出:
instance
class self_test {
static $instance;
public function __construct(){
self::$instance = 'dell';
}
static public function pentium(){
return self::$instance;//静态方法也可以继续访问静态变量,访问时需要加$
}
public function tank(){
return self::pentium();//访问静态方法pentium()
}
}
$str = new self_test();
echo $str->tank();
页面输出:
dell
2.self可以访问const定义的常量
class self_test {
const NAME = 'tancy';
public function tank(){
return self::NAME;
}
}
$str = new self_test();
echo $str->tank();
class self_test {
public $public;
private $private;
protected $protected;
public function __construct(){
$this->public = 'public';
$this->private = 'private';
$this->protected = 'protected';
}
public function tank(){
return $this->public;
}
public function dell(){
return $this->private;
}
public function datesrt(){
return $this->protected;
}
}
$str = new self_test();
echo $str->tank();
echo "</br>";
echo $str->dell();
echo "</br>";
echo $str->datesrt();