dqhendricks dqhendricks - 4 months ago 10
PHP Question

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.

Answer

No. PHP does not preserve that information.

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.