PHP:静态和非静态函数和对象

编程入门 行业动态 更新时间:2024-10-10 08:19:00
本文介绍了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:静态和非静态函数和对象

本文发布于:2023-10-31 06:44:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   函数   对象   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!