{"componentChunkName":"component---gatsby-theme-teklog-src-templates-post-tsx","path":"/php-7-2-and-allow-abstract-function-override","result":{"data":{"site":{"siteMetadata":{"siteUrl":"https://blog.tekmi.nl","author":{"name":"Teklog","avatar":"/img/tekmi-icon.svg"}}},"blogPost":{"frontmatter":{"title":"PHP 7.2 and Allow Abstract Function Override","slug":"php-7-2-and-allow-abstract-function-override","series":null,"date":"2017-12-14","datePretty":"14 December, 2017","coverImage":{"childImageSharp":{"fixed":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMFAf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAF2xmRTIpX/xAAaEAACAwEBAAAAAAAAAAAAAAAAAQISISIR/9oACAEBAAEFAuSyHLbSZ15jP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAECAQE/AVf/xAAbEAACAQUAAAAAAAAAAAAAAAAAARACERIhMv/aAAgBAQAGPwI1RFsmdOP/xAAdEAADAAICAwAAAAAAAAAAAAAAAREhMVFhgZGh/9oACAEBAAE/IVaj2vo7X2i+C6onMocbHkmd0//aAAwDAQACAAMAAAAQ+A//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPxBH/8QAFhEAAwAAAAAAAAAAAAAAAAAAARAR/9oACAECAQE/EKE//8QAHRABAAICAgMAAAAAAAAAAAAAAQARITFRYYGR0f/aAAgBAQABPxAjENCOfCUKLNK+S2t7QK9yjkHGKjyKWWmRckl2q3P/2Q==","width":1000,"height":750,"src":"/static/2a77fb7f7e83c968f4cbe061c5db86b6/a7715/sandExcavationArea.jpg","srcSet":"/static/2a77fb7f7e83c968f4cbe061c5db86b6/a7715/sandExcavationArea.jpg 1x"}}},"author":null,"authorAvatar":null},"timeToRead":2,"excerpt":"Introduction PHP 7.2 has been released on 30th of November 2017. The most important features have been listed out and briefly described, however there…","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 has been released on <a href=\"http://php.net/archive/2017.php#id2017-11-30-1\">30th of November 2017</a>. The most important features have been listed out and briefly described, however there is much more to be explored.</p>\n<p>Without further ado, let's dive into <a href=\"https://wiki.php.net/rfc/allow-abstract-function-override\">Allow Abstract Function Override</a> together.</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 CLI Stretch Container</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.0-cli-stretch <span class=\"token punctuation\">\\</span>\nphp php-7.2/abstract_function_override/index.php</code></pre></div>\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>As presented below, <em>Mammal::run</em> method has broaden the <em>$speed</em> parameter type from a <em>stdClass</em> to <em>no-type,</em> also known as <a href=\"https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Contravariant_method_argument_type\">Contravariant</a>. If you tried to do it other way around (Covariant), you would get <code class=\"language-text\">Fatal error: Declaration of Mammal::run(stdClass $speed) must be compatible with Animal::run($speed)</code>, because PHP doesn't yet support full-featured type variance.</p>\n<div class=\"gatsby-highlight\" data-language=\"php\"><pre class=\"language-php\"><code class=\"language-php\"><span class=\"token keyword\">abstract</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">Animal</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">abstract</span> <span class=\"token keyword\">public</span> <span class=\"token keyword\">function</span> <span class=\"token function\">run</span><span class=\"token punctuation\">(</span>\\<span class=\"token package\">stdClass</span> <span class=\"token variable\">$speed</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">abstract</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">Mammal</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Animal</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">abstract</span> <span class=\"token keyword\">public</span> <span class=\"token keyword\">function</span> <span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$speed</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Lion</span> <span class=\"token keyword\">extends</span> <span class=\"token class-name\">Mammal</span>\n<span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">public</span> <span class=\"token keyword\">function</span> <span class=\"token function\">run</span><span class=\"token punctuation\">(</span><span class=\"token variable\">$speed</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token double-quoted-string string\">\"Runs <span class=\"token interpolation\"><span class=\"token variable\">$speed</span></span> km/h\"</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Additionally, if you tried to run this piece of code in PHP 7.1 or earlier, you would encounter: <code class=\"language-text\">Fatal error: Can&#39;t inherit abstract function Animal::run() (previously declared abstract in Mammal)</code></p>\n<p>Feel free to verify it yourself by running it in the <a href=\"https://hub.docker.com/_/php/\">PHP 7.1 CLI Jessie Container</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.1.12-cli-jessie <span class=\"token punctuation\">\\</span>\nphp php-7.2/abstract_function_override/index.php</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>Without type variance, each abstract method in child classes must conform to the method signature of its parent class.</p>\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>At this point it doesn't look that beneficial, however, according to the author of this RFC, it will gain more uses when PHP will support a full-featured type variance.</p>"}},"pageContext":{"postId":"86f4a8b3-bc09-53d2-adf8-902bc93ff1c7","slug":"php-7-2-and-allow-abstract-function-override"}}}