Home >Backend Development >PHP Problem >What are the ways to access static methods in php

What are the ways to access static methods in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-06 10:39:083417browse

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

What are the ways to access static methods in php

How to access static methods in php:

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

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

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

Related learning recommendations: php programming (video)

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

Statement:
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