php在类中使用静态方法有几种方式

来自:互联网
时间:2020-11-04
阅读:

php在类中使用静态方法的方式:

1、使用self,代码如下

<?php  
class a {  
    private static function test() {  
        echo 'test';  
    }  
    public function test1() {  
        self::test();  
    }  
}  
$ab = new a();  
$ab->test1();//结果 test

2、使用类名,代码如下

<?php  
class a {  
    private static function test() {  
        echo 'test';  
    }  
    public function test1() {  
        a::test();  
    }  
}  
$ab = new a();  
$ab->test1();//结果 test

3、使用static,代码如下

<?php  
class a {  
    private static function test() {  
        echo 'test';  
    }  
    public function test1() {  
        static::test();  
    }  
}  
$ab = new a();  
$ab->test1();//结果 test
返回顶部
顶部