Doing some code reviews lately I came across a number of classes that have significant number of static methods in them... and I can't seem to grasp why? Hence my question:
最近做了一些代码评论,我遇到了许多具有大量静态方法的类......我似乎无法理解为什么?因此我的问题是:
What are the best practices regarding using static methods in PHP?
在PHP中使用静态方法的最佳实践是什么?
When would one want to use them and when would one shouldn't use them?
什么时候想要使用它们什么时候不应该使用它们?
What are specific difference in how runtime handles static methods? Do they affect performance or memory footprint?
运行时处理静态方法的具体区别是什么?它们会影响性能还是内存占用?
7 个解决方案
#1
23
Doing some code reviews lately I came across a number of classes that have significant number of static methods in them... and I can't seem to grasp why
最近我做了一些代码审查,我遇到了许多具有大量静态方法的类......我似乎无法理解为什么
PHP didn't have namespaces before 5.3, so all function/variables would be in global scope unless they belonged in some class. Putting them in a class as static members is a workaround for not having namespaces (and that's probably why you saw them in "significant" number)
PHP在5.3之前没有名称空间,因此所有函数/变量都在全局范围内,除非它们属于某个类。将它们作为静态成员放在一个类中是一种没有命名空间的解决方法(这可能就是为什么你在“重要”数字中看到它们)
Generally, they are used for functions that aren't much useful in individual objects, but has some use at class level (as said in other answers)
通常,它们用于在单个对象中没有多大用处的函数,但在类级别有一些用处(如其他答案中所述)
#2
5
Static methods are used for
静态方法用于
- functions that are related to the whole collection of objects of the given class (e.g. singleton pattern)
- 与给定类的整个对象集合相关的函数(例如,单例模式)
- functions that are not related to anything, but must be put under a class because of OO (e.g. utility classes)
- 与任何事物无关的函数,但由于OO(例如实用程序类)必须放在类下
#3
5
The best practice is avoid using them whenever possible, because they kill testability and maintainability. Two great reads that elaborate:
最佳做法是尽可能避免使用它们,因为它们会破坏可测试性和可维护性。精心制作的两篇精彩内容:
- Static considered harmful
- 静态认为有害
- Static Methods are Death to Testability
- 静态方法是可测试性的死亡
CLARIFICATION: There seems to be a lot of misunderstanding on this issue. Lack of dependency injection is the real problem. Directly calling static methods just happens to be the one of the most common ways of falling into that trap.
澄清:在这个问题上似乎存在很多误解。缺乏依赖注入是真正的问题。直接调用静态方法恰好是陷入陷阱的最常见方式之一。
#4
4
Static method doesn't require an instance (and may return one instead) and is more or less like a global function except for it is put in class' namespace (and therefore avoid collisions with other functions) and has access to class' private members.
静态方法不需要实例(并且可以返回一个实例)并且或多或少类似于全局函数,除非它被放在类的命名空间中(因此避免与其他函数冲突)并且可以访问类的私有成员。
So, use it whenever you're interested in these properties of the function.
因此,只要您对函数的这些属性感兴趣,就可以使用它。
#5
3
There is nothing PHP specific about the use of static methods.
关于静态方法的使用没有特定的PHP。
Static methods can be called directly on the class - no need for an instantiated object.
可以直接在类上调用静态方法 - 不需要实例化对象。
So their main use is for methods that are related to the classes functionality, but do not need an existing instance to be of use for other code.
因此,它们的主要用途是与类功能相关的方法,但不需要现有实例用于其他代码。
A common example would be a custom comparison method that can be passed to, say, the uasort() function to sort an array of objects of the class' type.
一个常见的例子是自定义比较方法,可以传递给uasort()函数,以对类类型的对象数组进行排序。
#6
2
you can use static methods for better performance. you don't need to create object for each user that using your web App and creating object with multiple methods and properties is slower and needs much more system resources.
您可以使用静态方法来获得更好的性能。您不需要为使用Web App的每个用户创建对象,并且使用多种方法和属性创建对象的速度较慢,需要更多的系统资源。
#7
1
You don't need to create an instance of the class to use its static methods.
您不需要创建类的实例来使用其静态方法。
#1
23
Doing some code reviews lately I came across a number of classes that have significant number of static methods in them... and I can't seem to grasp why
最近我做了一些代码审查,我遇到了许多具有大量静态方法的类......我似乎无法理解为什么
PHP didn't have namespaces before 5.3, so all function/variables would be in global scope unless they belonged in some class. Putting them in a class as static members is a workaround for not having namespaces (and that's probably why you saw them in "significant" number)
PHP在5.3之前没有名称空间,因此所有函数/变量都在全局范围内,除非它们属于某个类。将它们作为静态成员放在一个类中是一种没有命名空间的解决方法(这可能就是为什么你在“重要”数字中看到它们)
Generally, they are used for functions that aren't much useful in individual objects, but has some use at class level (as said in other answers)
通常,它们用于在单个对象中没有多大用处的函数,但在类级别有一些用处(如其他答案中所述)
#2
5
Static methods are used for
静态方法用于
- functions that are related to the whole collection of objects of the given class (e.g. singleton pattern)
- 与给定类的整个对象集合相关的函数(例如,单例模式)
- functions that are not related to anything, but must be put under a class because of OO (e.g. utility classes)
- 与任何事物无关的函数,但由于OO(例如实用程序类)必须放在类下
#3
5
The best practice is avoid using them whenever possible, because they kill testability and maintainability. Two great reads that elaborate:
最佳做法是尽可能避免使用它们,因为它们会破坏可测试性和可维护性。精心制作的两篇精彩内容:
- Static considered harmful
- 静态认为有害
- Static Methods are Death to Testability
- 静态方法是可测试性的死亡
CLARIFICATION: There seems to be a lot of misunderstanding on this issue. Lack of dependency injection is the real problem. Directly calling static methods just happens to be the one of the most common ways of falling into that trap.
澄清:在这个问题上似乎存在很多误解。缺乏依赖注入是真正的问题。直接调用静态方法恰好是陷入陷阱的最常见方式之一。
#4
4
Static method doesn't require an instance (and may return one instead) and is more or less like a global function except for it is put in class' namespace (and therefore avoid collisions with other functions) and has access to class' private members.
静态方法不需要实例(并且可以返回一个实例)并且或多或少类似于全局函数,除非它被放在类的命名空间中(因此避免与其他函数冲突)并且可以访问类的私有成员。
So, use it whenever you're interested in these properties of the function.
因此,只要您对函数的这些属性感兴趣,就可以使用它。
#5
3
There is nothing PHP specific about the use of static methods.
关于静态方法的使用没有特定的PHP。
Static methods can be called directly on the class - no need for an instantiated object.
可以直接在类上调用静态方法 - 不需要实例化对象。
So their main use is for methods that are related to the classes functionality, but do not need an existing instance to be of use for other code.
因此,它们的主要用途是与类功能相关的方法,但不需要现有实例用于其他代码。
A common example would be a custom comparison method that can be passed to, say, the uasort() function to sort an array of objects of the class' type.
一个常见的例子是自定义比较方法,可以传递给uasort()函数,以对类类型的对象数组进行排序。
#6
2
you can use static methods for better performance. you don't need to create object for each user that using your web App and creating object with multiple methods and properties is slower and needs much more system resources.
您可以使用静态方法来获得更好的性能。您不需要为使用Web App的每个用户创建对象,并且使用多种方法和属性创建对象的速度较慢,需要更多的系统资源。
#7
1
You don't need to create an instance of the class to use its static methods.
您不需要创建类的实例来使用其静态方法。