{"id":58,"date":"2009-01-07T17:00:03","date_gmt":"2009-01-07T16:00:03","guid":{"rendered":"http:\/\/muratyaman.co.uk\/wp\/?p=58"},"modified":"2020-04-01T12:40:40","modified_gmt":"2020-04-01T11:40:40","slug":"php-magic-methods","status":"publish","type":"post","link":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/2009\/01\/php-magic-methods\/","title":{"rendered":"PHP: magic methods"},"content":{"rendered":"<p>Regarding __toString<\/p>\n<pre lang=\"php\">\r\n\r\nclass my_tag_A{\r\n\r\n\tpublic $id='';\r\n\tpublic $href='';\r\n\tpublic $target='';\r\n\tpublic $class='';\r\n\t\r\n\tpublic $label='';\r\n\t\r\n\tfunction __construct($href, $label){\r\n\t\t$this->href = $href;\r\n\t\t$this->label = $label;\r\n\t}\r\n\t\r\n\tpublic function __toString(){\r\n\t\treturn '<a ' . $this->nz_arr(\r\n\t\t\tarray('id', 'href', 'target', 'class')) . ' >' \r\n\t\t\t. $this->label . '<\/a>';\r\n\t}\r\n\t\r\n\tfunction nz_arr($attrib_arr){\r\n\t\t$s = '';\r\n\t\tforeach($attrib_arr as $attrib){\r\n\t\t\t$s .= $this->nz($attrib);\r\n\t\t}\r\n\t\treturn $s;\r\n\t}\r\n\r\n\t\/**\r\n\t * Print the tag attribute if it is not blank, such as id=\"$this->id\"\r\n\t * @param string $attrib\r\n\t * @return string\r\n\t *\/\r\n\tfunction nz($attrib){\r\n\t\t$s = '';\r\n\t\tif($this->$attrib != '') $s = $attrib .' = \"' . $this->$attrib . '\"';\r\n\t\treturn $s;\r\n\t}\r\n\r\n\t\/\/This causes RECURSION because of parsing between double quotes.\r\n\t\/\/This is a very UNEXPECTED behaviour!\r\n\tfunction nz_($attrib){\r\n\t\t$s = '';\r\n\t\tif($this->$attrib != '') $s = \"$attrib = \\\"$this->$attrib\\\"\";\r\n\t\treturn $s;\r\n\t}\r\n\t\r\n}\/\/end  class\r\n\r\n\/\/usage\r\n$a = new my_tag_A('abc.php', 'ABC'); $a->target = '_blank';\r\necho $a;\r\n\/\/prints:\r\n\/\/    <a href=\"abc.php\" target=\"_blank\" rel=\"noopener noreferrer\">ABC<\/a>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Regarding __toString<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[65,26],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-technology","tag-oop","tag-php"],"_links":{"self":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/58","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":3,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/58\/revisions\/954"}],"wp:attachment":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}