什么时候在 PHP 中使用静态类? [英] When to use static classes in PHP?

查看:80
本文介绍了什么时候在 PHP 中使用静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
何时使用静态类与实例化类

我在理解 PHP 中静态类与普通"类的优缺点时遇到了一些麻烦,因为我似乎可以对两者做同样的事情.

I'm having a little trouble understanding the advantages/disadvantages of static vs "normal" classes in PHP, since it seems that I'm able to do the same thing with both.

如果我能够在类中拥有静态变量并使用静态方法轻松获取/修改它们,为什么我什至需要类实例?

If I'm able to have static variables in classes and get/modify them easily with static methods, why do I even need class instances?

推荐答案

Static 一个类的实例只发生一次,它的变量对任何个实例可用班上.实例有自己的单独值,其他实例无法访问这些值,除非它们被标记为 static.

The Static instance of a class only happens once, and its variables are available to any instance of the class. Instances have their own individual values which are not accessible to other instances, except where they are marked static.

当类的多个实例时,实例很有用.

Instances are useful when there will be more than one instance of the class.

这篇关于什么时候在 PHP 中使用静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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