There are several ways to use static methods in php classes

coldplay.xixi
Release: 2023-03-07 09:28:01
Original
2714 people have browsed it

How to use static methods in PHP classes: 1. Use self, the code is [self::test();]; 2. Use the class name, the code is [public function test1()a:: test()]; 3. Use static, the code is [static::test()].

There are several ways to use static methods in php classes

How to use static methods in php classes:

1. Use self, the code is as follows

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

2. Use the class name, the code is as follows

<?php  
class a {  
    private static function test() {  
        echo &#39;test&#39;;  
    }  
    public function test1() {  
        a::test();  
    }  
}  
$ab = new a();  
$ab->test1();//结果 test
Copy after login

3. Use static, the code is as follows

<?php  
class a {  
    private static function test() {  
        echo &#39;test&#39;;  
    }  
    public function test1() {  
        static::test();  
    }  
}  
$ab = new a();  
$ab->test1();//结果 test
Copy after login

Related free learning recommendations: php programming( video)

The above is the detailed content of There are several ways to use static methods in php classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!