It is okay to implement \Countable in a PHP class that is not meant to be iterable?

时间:2015-06-26 10:03:59

标签: php spl countable

I have a MultipleException class that collects exceptions (e.g. multiple errors in form fields) and implements countable, but I don't want it to be iterable like a list, cause it is actually an exception (you would never throw a list).

Is this okay in terms of software design? Or am I misusing the SPL Countable interface?

Which is the right way?

Thanks

1 个答案:

答案 0 :(得分:4)

没关系。

Countable是这样的,所以你的任何类都可以自定义用实例调用count()返回的值。 完全与可迭代的类分开。