静态方法:考虑到PHP 5.3后期静态绑定,它们还是不好吗? [英] Static methods: are they still bad considering PHP 5.3 late static binding?

查看:98
本文介绍了静态方法:考虑到PHP 5.3后期静态绑定,它们还是不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您搜索静态方法不好的原因,您首先发现的是因为您在单元测试时不能覆盖它。

If you search on the reasons why static methods are bad the first thing you find it is because you can't override it when you are unit testing.

所以考虑到PHP 5.3,您可以使用 static ::

So is this still true considering in PHP 5.3 you can do whatever you want with the introduction of static::?

http ://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html

请注意,他甚至解释了如何使用单身没有任何测试问题:

Note he explains even how to use singleton without any testing problem:

  • http://sebastian-bergmann.de/archives/882-Testing-Code-That-Uses-Singletons.html

推荐答案

如果你有一个静态成员函数,它通常可以是一个免费的函数。通常的反应是,编码器选择了一个静态成员函数,只是因为一切都必须在一个对象的神话。

If you have a static member function, it could usually be a free function. The usual reaction is then that the coder has opted for a static member function only because of the myth that "everything must be in an object".

这就是为什么人们不鼓励他们

That's why people discourage them.

而且,由于这不是一个非常有说服力的论据,所以这些人指的是单元测试。不知道他们会做什么。

And, because it's not a very convincing argument, those people pointed to unit testing instead. Not sure what they'll do now.

这篇关于静态方法:考虑到PHP 5.3后期静态绑定,它们还是不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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