Rationale behind SimpleXMLElement's handling of text values in addChild and addAttribute

前端 未结 6 948
别那么骄傲
别那么骄傲 2020-12-01 09:16

Isn\'t that an inconsistent behavior? (PHP 5.2.6)

\');

$a->addAttribute(\'b\', \'One & Two\');
//$a-&         


        
6条回答
  •  伪装坚强ぢ
    2020-12-01 09:46

    "Let's say you had a database of text where all the ampersands were already escaped."

    If you're doing this, you're doing it wrong. Data should be stored in its most accurate form, not munged for whatever type of output you're currently using. This is even worse if you actually store blobs of (valid) HTML in the database. Using addChild() and grabbing the data out again will destroy your HTML; no sensible library exhibits such horrible asymmetry.

    addChild() not encoding your text for you is completely counter-intuitive. What is the point in an API that doesn't protect you from this? It's like json_encode() barfing if you use a double quote in one of your values.

    Anyway, to answer the original question: Obviously, I too think it's not a good decision. I do think it's consistent with a lot of PHP's design decisions, which is to fulfill someone's idea of what is "quicker", rather than being correct.

提交回复
热议问题