Is there any way to reset all static properties of a particular class?(有没有办法重置特定类的所有静态属性?)
问题描述
您可能知道,静态属性使测试变得困难.有没有办法将特定类的所有静态属性重置回初始状态?理想情况下,这不需要为每个类自定义代码,但可以通过继承以一般方式使用,或者完全从类外部使用.
Static properties make testing hard as you probably know. Is there no way to reset all static properties of a particular class back to their initial state? Ideally this would not require custom code for each class, but could be used in a general way by inheritance, or from outside of the class completely.
请不要回复诸如不要使用静态属性"之类的内容.谢谢.
Please do not reply with something like, "don't use static properties". Thanks.
推荐答案
没有.PHP 不保留该信息.
No. PHP does not preserve that information.
我在玩弄 ReflectionClass
和 ::getDefaultProperties
和 ::getStaticProperties
,但它们只返回当前状态.
I was toying around with ReflectionClass
and ::getDefaultProperties
and ::getStaticProperties
, but they only return the current state.
您必须使用默认值创建一个数组,然后手动遍历它们并重置您的类属性.
You will have to create an array with the default values, then manually foreach over them and reset your class attributes.
这篇关于有没有办法重置特定类的所有静态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法重置特定类的所有静态属性?


基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01