{"id":456,"date":"2010-12-30T00:49:43","date_gmt":"2010-12-29T23:49:43","guid":{"rendered":"https:\/\/d-mueller.de\/blog\/?p=456"},"modified":"2010-12-30T00:49:43","modified_gmt":"2010-12-29T23:49:43","slug":"php-typehinting-schon-heute","status":"publish","type":"post","link":"https:\/\/d-mueller.de\/blog\/php-typehinting-schon-heute\/","title":{"rendered":"PHP Typehinting schon heute"},"content":{"rendered":"<p>Ein Traum!<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nfunction exampleString(string $string) {\r\n    echo &quot;&#039;$string&#039; is a string!\\n&quot;;\r\n}\r\n\r\nfunction exampleInt(int $int) {\r\n    echo &quot;$int is an int!\\n&quot;;\r\n}\r\n\r\nfunction exampleFloat(float $float) {\r\n    echo &quot;$float is a float!\\n&quot;;\r\n}\r\n\r\nfunction exampleBool(bool $bool) {\r\n    var_export($bool);\r\n    echo &quot; is a bool!\\n&quot;;\r\n}\r\n\r\nexampleString(&quot;Hello World!&quot;); \/\/&#039;Hello World!&#039; is a string!\r\nexampleInt(1); \/\/1 is an int!\r\nexampleFloat(1.2); \/\/1.2 is a float!\r\nexampleBool(false); \/\/false is a bool!\r\n\r\nexampleBool(&quot;not really bool&quot;);\r\n\/*\r\nCatchable fatal error: Argument 1 passed to exampleBool() must be an instance of Bool, string given, called in D:\\xampp\\htdocs\\typehint\\example.php on line 23 and defined in D:\\xampp\\htdocs\\typehint\\example.php on line 13\r\n*\/\r\n<\/pre>\n<p>Und das alles gibts nicht erst in 3 Jahren sondern bereits heute! <a href=\"http:\/\/www.phpclasses.org\/package\/4195-PHP-Implement-type-hinting-support-for-base-PHP-types.html\">Tada!<\/a> Leider &#8211; ihr habts geahnt &#8211; hat die Sache einen geh\u00f6rigen Haken. Erstmal dass ich mich mit<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nrequire_once &#039;typehintclass.lib.php&#039;;\r\n<\/pre>\n<p>rumschlagen muss und zweitens (viel schlimmer) &#8211; das Ganze ist logischerweise kein valides PHP und erzeugt einen Error. Die typehint-lib bringt also einen eigenen Errorhandler mit, der auf f\u00fcrchterlich unperformante und h\u00e4ssliche Weise den Fehler auseinandernimmt und dann entweder failed oder (wenn Typ korrekt war) den Fehler unter den Tisch kehrt und weitermacht.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n\r\npublic function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {\r\n\tif ($errno == E_RECOVERABLE_ERROR) {\r\n\t\tif ($parsedError = $this-&gt;parseErrorString($errstr)) {\r\n\t\t\t$backtrace = debug_backtrace();\r\n\t\t\t$value = @$backtrace[1][&#039;args&#039;][$parsedError[&#039;argnum&#039;]-1];\r\n\t\t\tif ($this-&gt;solveTypeHintFailure($parsedError[&#039;typehint&#039;], $value)) {\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\tif ($this-&gt;oldErrorHandler) {\r\n\t\treturn call_user_func($this-&gt;oldErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);\r\n\t} else {\r\n\t\treturn false;\r\n\t}\r\n}\r\n<\/pre>\n<p>Tja, anders gehts leider nicht. Aber w\u00e4r sch\u00f6n gewesen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ein Traum! function exampleString(string $string) { echo &quot;&#039;$string&#039; is a string!\\n&quot;; } function exampleInt(int $int) { echo &quot;$int is an int!\\n&quot;; } function exampleFloat(float $float) { echo &quot;$float is a float!\\n&quot;; } function exampleBool(bool $bool) { var_export($bool); echo &quot; is &hellip; <a href=\"https:\/\/d-mueller.de\/blog\/php-typehinting-schon-heute\/\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-456","post","type-post","status-publish","format-standard","hentry","category-php","category-webdev"],"_links":{"self":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":0,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"wp:attachment":[{"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/d-mueller.de\/blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}