当前位置:Gxlcms > PHP教程 > php访问静态方法有哪些方式

php访问静态方法有哪些方式

时间:2021-07-01 10:21:17 帮助过:41人阅读

php访问静态方法的方式:1、使用self,代码为【self::test();】;2、使用类名,代码为【a::test()】;3、使用static,代码为【public function test1(){static::test()}】。

php访问静态方法的方式:

1:使用self,代码如下

  1. <?php
  2. class a {
  3. private static function test() {
  4. echo 'test';
  5. }
  6. public function test1() {
  7. self::test();
  8. }
  9. }
  10. $ab = new a();
  11. $ab->test1();//结果 test

2:使用类名,代码如下

  1. <?php
  2. class a {
  3. private static function test() {
  4. echo 'test';
  5. }
  6. public function test1() {
  7. a::test();
  8. }
  9. }
  10. $ab = new a();
  11. $ab->test1();//结果 test

3:使用static,代码如下

  1. <?php
  2. class a {
  3. private static function test() {
  4. echo 'test';
  5. }
  6. public function test1() {
  7. static::test();
  8. }
  9. }
  10. $ab = new a();
  11. $ab->test1();//结果 test

相关学习推荐:php编程(视频)

以上就是php访问静态方法有哪些方式的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行