{"componentChunkName":"component---gatsby-theme-teklog-src-templates-post-tsx","path":"/php-7-2-and-object-typehint","result":{"data":{"site":{"siteMetadata":{"siteUrl":"https://blog.tekmi.nl","author":{"name":"Teklog","avatar":"/img/tekmi-icon.svg"}}},"blogPost":{"frontmatter":{"title":"PHP 7.2 and Object Typehint","slug":"php-7-2-and-object-typehint","series":null,"date":"2017-10-29","datePretty":"29 October, 2017","coverImage":{"childImageSharp":{"fixed":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC7UlEQVQ4y5VSbUhTYRS+d0I/DOxHkBDahxVqJSWKJmmUjEITP0jX5gc658c2tzvndvd1r5vbtGmKZZJpSrlMNEUzdeFclhYV9VMIiuhPf4T+1L/Kbe/p3dVdUirowOF9OO95n3Pe8xyC2LQDB48TK6+Bw3kFjYRcNSho63gVMTEDEd8BSAAgsJMeHwhcnW8EFdIu8kTyBS7/8xcgSJIkeKtT3ORxs31RQPyHmdh5Pr+gyEAQtfINsviEFAJX58uIJLaUeuWApkEz3K/WjkyrGkfmlZR7qk55uw93p8oWSpNxGpcf6nxLlZNJqaEgh6Wyqzla/dgKbX4YZFu80NL6HKzOZbA5n4LjyjK0tr8E1r4IWnryZ51i0IuJhVvIEhJPEZ6VjQoKym00WZeAdbwAA+sFI/s4oDOM+xnbtL+13ed3upb8jPWe3+qYCzTbvcC2+IBqmkC5eZR6s1OC0NLj3AwqpN2ZZssk2Aw9wNK96wx9C7HsJKqstqIGtQvKKgzospiCc9m5qLikFpWWa1CxSL6u1rpBSd3/EXfoRCbXoavzbQQnjHrU5LAMwDVV3nqHoghcSjFidF2oqsYOYokKci+KobBQApdK6qG0rAGlp2cgkViByfr9OuM8nDlbauQINboxwYZC+hz8RfzNBaDNswGaWUB68yOgtH1IruxA1TV2xNpmEGOdQ7TpDkhlDMLjCNBmD8hVdyEmNkHEEUZHx4UFEYjLnDcs1gVotj8BEzMXNFhmA+bmhYCJ9QSNjCfYZJgJanRTAappKoC7wjFvSBw4nSUawu938cIcPpIWhjuyhTKqVt7/kTbPgNWBBbL7uALbvVH/AMorOz4dPZZlwe9282TDo9+4c9/+pN/Fj0lNy6/My28akJS3Paus7l6tkl1/X1HVtVoiti0Lz9cOxSdk1IfW94/b3t37jsdrX7ctKUHsxL4H+97QhLBHhReaX5W/WfgyJjaR7On7QP4reci9RkZGRnHEknI7H/8FghltjbO+4mkAAAAASUVORK5CYII=","width":1000,"height":625,"src":"/static/f20334233cd400320dda48dd89d12a97/31987/php_object.png","srcSet":"/static/f20334233cd400320dda48dd89d12a97/31987/php_object.png 1x"}}},"author":null,"authorAvatar":null},"timeToRead":2,"excerpt":"Introduction PHP 7.2 is around the corner.  On the 26th of October 2017, the 5th version of PHP Release Candidate has been announced, which was…","html":"<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>PHP 7.2 is around the corner.  On the 26th of October 2017, the 5th version of PHP <a href=\"https://github.com/php/php-src/blob/php-7.2.0RC5/NEWS\">Release Candidate</a> has been announced, which was preceded by 3 alpha, 3 beta and 4 RC versions. First alpha version was announced on the <a href=\"https://wiki.php.net/todo/php72#timetable\">8th of June 2017</a> and the final version is about to be released by the end of November 2017.</p>\n<p>If you are impatient and curious as I am, it's right time to take a sneak peak at what PHP team is cooking for us. You can track all implemented features by studying PHP <a href=\"https://wiki.php.net/rfc#php_72\">Request For Comments</a> wiki page.</p>\n<p>Without further ado, let's explore the first feature together, an amazing <a href=\"https://wiki.php.net/rfc/object-typehint\">Object Typehint</a></p>\n<h2 id=\"assumptions\" style=\"position:relative;\"><a href=\"#assumptions\" aria-label=\"assumptions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Assumptions</h2>\n<ul>\n<li><a href=\"https://docs.docker.com/engine/installation/#supported-platforms\">Docker is installed</a>.</li>\n</ul>\n<h2 id=\"try-it-yourself\" style=\"position:relative;\"><a href=\"#try-it-yourself\" aria-label=\"try it yourself permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Try it yourself</h2>\n<p>Clone <strong>blogging-phplatest</strong> repository</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">git</span> clone https://github.com/tekmi/blogging-phplatest\n<span class=\"token builtin class-name\">cd</span> blogging-phplatest</code></pre></div>\n<p>Run docker container, based on the <a href=\"https://hub.docker.com/_/php/\">php 7.2.0 RC5 image</a>.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">docker run -it --rm <span class=\"token punctuation\">\\</span>\n-v <span class=\"token string\">\"<span class=\"token environment constant\">$PWD</span>\"</span>:/usr/src/php-7.2 <span class=\"token punctuation\">\\</span>\n-w /usr/src/php-7.2 <span class=\"token punctuation\">\\</span>\nphp:7.2.0RC5-cli-stretch <span class=\"token punctuation\">\\</span>\nphp php-7.2/object_typehint/index.php</code></pre></div>\n<p><strong>Update Dec 2017</strong>: If you experience problems with image <em>php:7.2.0RC5-cli-stretch</em>, try <em>php:7.2.0-cli-stretch</em> instead.</p>\n<h2 id=\"explanation\" style=\"position:relative;\"><a href=\"#explanation\" aria-label=\"explanation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Explanation</h2>\n<p>PHP 7.0 introduced scalar types for parameters as well as return types. PHP 7.1 on top of this added <a href=\"http://php.net/manual/en/migration71.new-features.php#migration71.new-features.nullable-types\">Nullable types</a>, however there was still not possible to typehint objects.</p>\n<p>PHP 7.2 for the rescue - it will soon be possible to use new <strong>object</strong> typehint as shown below:</p>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">function</span> <span class=\"token function\">moves</span><span class=\"token punctuation\">(</span>object <span class=\"token variable\">$obj</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span> string\n<span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token function\">get_class</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$obj</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>If you pass string, null or anything else that is not an object, you will encounter fatal error similar to: <code class=\"language-text\">Fatal error: Uncaught TypeError: Argument 1 passed to moves() must be an object, string given</code></p>\n<p>On the other hand, if you pass valid objects, as presented below, everything will work like a charm.</p>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token function\">moves</span><span class=\"token punctuation\">(</span><span class=\"token function\">json_decode</span><span class=\"token punctuation\">(</span><span class=\"token single-quoted-string string\">'{}'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token function\">moves</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\"><span class=\"token punctuation\">\\</span>stdClass</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token function\">moves</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">class</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<h2 id=\"how-did-we-do-it-until-now\" style=\"position:relative;\"><a href=\"#how-did-we-do-it-until-now\" aria-label=\"how did we do it until now permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How did we do it until now</h2>\n<p>Most probably parameter <em>$obj</em> won't be type-hinted (if we intend to accept scalars and objects) and we should check for an object - otherwise <em>get_class</em> would fire: <code class=\"language-text\">Warning: get_class() expects parameter 1 to be object, string given</code></p>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">function</span> <span class=\"token function\">moves</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$obj</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span> string\n<span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token function\">is_object</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$obj</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">?</span> <span class=\"token function\">get_class</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$obj</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">:</span> <span class=\"token single-quoted-string string\">''</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>By using <strong>object</strong> typehint, code should be easier to understand and errors detected quicker. It's a great addition to PHP language which I'm awaiting with impatience.</p>"}},"pageContext":{"postId":"332c5545-1875-5162-a998-bd4d1d55c38c","slug":"php-7-2-and-object-typehint"}}}