php静态函数 [英] php static function

查看:126
本文介绍了php静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于php中的静态函数的问题。

I have a question regarding static function in php.

我们假设我有一个类

class test {
    public function sayHi() {
        echo 'hi';
    }
}

:sayHi(); 它没有问题。

if I do test::sayHi(); it works without a problem.

class test {
    public static function sayHi() {
        echo 'hi';
    }
}

test :: sayHi );

test::sayHi(); works as well.

第一类和第二类之间的区别是什么?

What are the differences between first class and second class?

一个静态函数有什么特别之处?

What is special about a static function?

推荐答案

在第一个类中, sayHi )实际上是一个作为静态方法调用的实例方法,你逃避它,因为 sayHi()从不引用 $ this

In the first class, sayHi() is actually an instance method which you are calling as a static method and you get away with it because sayHi() never refers to $this.

静态函数与类关联,而不是类的实例。因此, $ this 不能从静态上下文( $ this 不指向任何对象)。

Static functions are associated with the class, not an instance of the class. As such, $this is not available from a static context ($this isn't pointing to any object).

这篇关于php静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆