PHP:静态和非静态函数和对象 [英] PHP: Static and non Static functions and Objects

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

问题描述

这些对象调用之间有什么区别?

What's the difference between these object callings?

非静态:

$var = new Object;
$var->function();

静态:

$var = User::function();

class内,为什么还要对函数使用static属性?

And also inside a class why should I use the static property for functions?

示例:

static public function doSomething(){
    ...code...
}

推荐答案

静态函数,根据定义,不能也不依赖于该类的任何实例属性.也就是说,它们不需要类的实例来执行(因此,可以如您所显示的那样执行,而无需先创建实例).从某种意义上讲,这意味着该函数不必(也永远不需要)依赖于类的成员或方法(公共或私有).

Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed as you've shown without first creating an instance). In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.

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

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