开发者

Reason for DOMElement::getAttribute() returning an empty string when there is no such attribute

In the php docs it says:

R开发者_如何学Goeturn Values

The value of the attribute, or an empty string if no attribute with the given name is found.

Does anybody know the reason for returning an empty string instead of just NULL?


The point is that the DOM specification is not platform-specific. It was written to be implemented in a wide variety of programming languages, so it tends not to assume the existence of types such as null. The W3C can assume the existence of a string type, so it's safe to use an empty string.

So the specification reads as this:

DOMString                 getAttribute(in DOMString name);

So a string is the expected type on a cross-platform basis. PHP's DOMDocument conforms to this.


I don't think it really matters what it returns. To check the validity of the result you need to call DOMElement::hasAttribute() anyway.

It could be just because they want to keep it simple and have everything return strings instead of a magic value of a different type (like null).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜