{"id":463,"date":"2022-08-30T15:01:46","date_gmt":"2022-08-30T15:01:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/is-undefined-a-data-type-in-php-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:46","modified_gmt":"2022-08-30T15:01:46","slug":"is-undefined-a-data-type-in-php-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/is-undefined-a-data-type-in-php-collection-of-common-programming-errors\/","title":{"rendered":"Is undefined a data-type in php?-Collection of common programming errors"},"content":{"rendered":"<p>(might be a bit over the top)<\/p>\n<p>Here are two fairly &#8220;simple&#8221; ways to mimic your own <em>undefined<\/em> type. With some overhead for a function call it can be available to your code without using the keyword global.<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>the output (PHP 5.3.23):<\/p>\n<pre><code>undef1 in string context : string(9) \"undefined\" \/\/ :)\nundef1 in boolean context: bool(true)            \/\/ :(\nundef1 compared to itself: bool(true)            \/\/ :)\n\nundef2 in string context : string(0) \"\"    \/\/ :( ?\nundef2 in boolean context: bool(false)     \/\/ :)\nundef2 compared to itself: bool(true)      \/\/ :)\n<\/code><\/pre>\n<p>In either case it seems ticky to get the thing to feel like JavaScript.<\/p>\n<p>To get the object to evaluate to boolean false the second solution uses a hack with the way PHP handles <em>SimpleXMLElement objects<\/em>. However that same hack can backfire if some code alters the XML value of your <em>undef2<\/em> object.<\/p>\n<p>Sadly the magic function __<em>toString()<\/em> becomes ironically useless when extending the SimpleXMLElement class. Maybe there is another function to overload that will return a custom string but I cannot find it. Though maybe an empty string is actually more practical.<\/p>\n<p>There are PECL operator overloads that could maybe get the <em>undef1<\/em> class to evaluate to boolean false, but the extension is beta and might not be available to your PHP code.<\/p>\n<p><em>Misc notes:<\/em><\/p>\n<p>It seems that an <em>undefined<\/em> type would make a lot of sense for custom classes that implement ArrayAccess, where it is be possible to:<\/p>\n<ul>\n<li>ignore creation of elements if the <em>$value<\/em> === <em>undefined<\/em><\/li>\n<li>return <em>undefined<\/em> if a key or index is not defined<\/li>\n<\/ul>\n<p>In the example above the class is not restricted to a singleton. You can make it behave that way if you wish: Creating the Singleton design pattern in PHP5<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:43:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>(might be a bit over the top) Here are two fairly &#8220;simple&#8221; ways to mimic your own undefined type. With some overhead for a function call it can be available to your code without using the keyword global. the output (PHP 5.3.23): undef1 in string context : string(9) &#8220;undefined&#8221; \/\/ \ud83d\ude42 undef1 in boolean context: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-463","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=463"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/463\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}