<?php
class Demo{
public static $static;
public $Nostatic;
public function __construct(){
self::$static = "static";
$this->Nostatic = "Nostatic";
}
public static function get(){
return __CLASS__;
}
public function show(){
return "this is function show with ".$this->Nostatic;
}
public function test(){
echo Demo::$static."<br/>"; //使用类名调用静态属性
echo Demo::get()."<br/>"; //使用类名调用静态属性
echo Demo::show()."<br/>"; //使用类名调用静态属性
echo self::$static."<br/>"; //self调用静态属性
echo self::show()."<br/>"; //self调用非静态方法
echo self::get()."<br/>"; //self调用静态方法
echo static::$static."<br/>";//static调用静态属性
echo static::show()."<br/>";//static调用非静态方法
echo static::get()."<br/>"; //static调用静态方法
}
}
$obj = new Demo();
$obj->test();
输出结果:
static
Demo
this is function show with Nostatic
static
this is function show with Nostatic
Demo
static
this is function show with Nostatic
Demo
<?php
class A{
static function getClassName(){
return "this is class A";
}
static function testSelf(){
echo self::getClassName();
}
static function testStatic(){
echo static::getClassName();
}
}
class B extends A{
static function getClassName(){
return "this is class B";
}
}
B::testSelf();
echo "<br/>";
B::testStatic();
<?php
class A{
public static $static;
public $Nostatic;
public function __construct(){
self::$static = "static";
$this->Nostatic = "Nostatic";
}
public static function staticFun(){
return self::$static;
}
public function noStaticFun(){
return "this is function show with ".$this->Nostatic;
}
}
class B extends A{
static function testS(){
echo parent::staticFun();
}
function testNoS(){
echo parent::noStaticFun();
}
}
$obj = new B();
$obj->testS();
echo "<br/>";
$obj->testNoS();
输出结果
static
this is function show with Nostatic
在文章的最后,我们分析一个手册上的例子
<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
?>
输出结果
A
C
C
我们单独拿出test方法进行分析:
public static function test() {
A::foo();
parent::foo();
self::foo();
}
<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
// public static function who() {
// echo __CLASS__."\n";
// }
}
C::test();
?>
输出结果:
A B B
注2:补充解释上面的(3)
<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function foo() {
echo "fooB"."\n";
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function foo() {
echo "fooC"."\n";
}
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
?>