{"id":1629,"date":"2016-02-22T10:57:13","date_gmt":"2016-02-22T09:57:13","guid":{"rendered":"https:\/\/www.entropywins.wtf\/blog\/?p=1629"},"modified":"2018-07-31T13:03:02","modified_gmt":"2018-07-31T12:03:02","slug":"missing-in-php7-collections","status":"publish","type":"post","link":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/","title":{"rendered":"Missing in PHP7: Collections"},"content":{"rendered":"<p>This is the fourth post in my <a href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/01\/30\/missing-in-php7\/\">Missing in PHP7 series<\/a>. The previous one is about <a href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/03\/missing-in-php7-value-objects\/\">Value Objects<\/a>.<\/p>\n<p>In this post I&#8217;ll outline some problems PHP has with regards to collections, the implications of those problems, and a few mitigation strategies.<\/p>\n<p><strong>PHP arrays<\/strong><\/p>\n<p>PHPs main collection type is the associative array. These arrays can be used as maps and lists, and form the jack of all trades that is used for nearly all PHP collections.<\/p>\n<pre class=\"lang:php decode:true \">$oldArray = array( 'pink' =&gt; 'fluffy', 'unicorns' ); \/\/ PHP &lt;= 5.3\n$newArray = [ 'pink' =&gt; 'fluffy', 'unicorns' ]; \/\/ PHP &gt; 5.3<\/pre>\n<p>It&#8217;s possible to type hint against array, including return types as of PHP7.<\/p>\n<pre class=\"lang:php decode:true\">function magicArrayModify( array $inputValues ): array {\n    \/\/ This is where the magic happens\n    return $outputValues;\n}<\/pre>\n<p><strong>Problem 1: lackluster type hinting<\/strong><\/p>\n<p>There are collections that contain values of multiple types, such as maps containing configuration. Many collections however are meant to hold only a specific type of value. While for scalar values, it is possible to type hint and get all the benefits from static code analysis, the collections, the only thing you can say with code is that they are collections.<\/p>\n<p>Luckily there is a standardized way to specify such types in doc blocks, which is understood by many tools.<\/p>\n<pre class=\"lang:php decode:true \">\/**\n * @param string[] $inputValues\n * @return string[]\n *\/\nfunction magicArrayModify( array $inputValues ): array {}<\/pre>\n<p>This approach has some drawbacks. Firstly, it&#8217;s documentation, which can easily get out of sync with the actual code. Secondly, while it provides much-needed information to static code analysis tools, PHP itself ignores violations of the contracts specified in these docs.<\/p>\n<p><strong>Trick: improved type hinting<\/strong><\/p>\n<p>As of PHP 5.6, there is a way to type hint against arrays of a specific type with code for parameters. This is done by means of <a href=\"http:\/\/php.net\/manual\/en\/functions.arguments.php#functions.variable-arg-list\">variable-length argument lists<\/a>.<\/p>\n<pre class=\"lang:php decode:true\">function haveSomeStrings( string ...$inputValues ) {}\n\nhaveSomeStrings( ...$awesomeStrings );<\/pre>\n<p>You can easily refactor existing code to follow this approach. The only thing you need to do is add the unpacking operator to the arguments in the calls to the function. When I first tried this out, I was surprised by how many of the callers did not actually pass in a variable length collection. Most of these being in tests. So far, I&#8217;ve removed more no longer needed arrays, than having to add the unpacking operator. Example:<\/p>\n<pre class=\"lang:php decode:true \">function testSomething() {\n    haveSomeStrings( [ 'foo', 'bar', 'baz' ] );\n    \/\/ Some assertions\n}\n\nfunction testSomething() {\n    haveSomeStrings( 'foo', 'bar', 'baz' );\n    \/\/ Some assertions\n}<\/pre>\n<p>Some people argue you should wrap all collections (be it in PHP or not), which certainly helps with the lack of proper collection type hinting in PHP. This trick is especially nice for the constructors of such wrappers.<\/p>\n<p>The unpacking operator also works for traversables, which makes this approach partially mitigate the next problem.<\/p>\n<p><strong>Problem 2: arrays and Traversable<\/strong><\/p>\n<p>PHPs <code>Traversable<\/code> interface is the base interface for everything that can be looped though. It gets extended by <code>Iterator<\/code> and <code>Generator<\/code> and is implemented by dozens of classes in PHPs standard library. Iterators are awesome, as I&#8217;ve written about before in <a href=\"https:\/\/www.entropywins.wtf\/blog\/2015\/08\/19\/lazy-iterators-in-php-and-python\/\">Lazy iterators in PHP and Python<\/a> and <a href=\"https:\/\/www.entropywins.wtf\/blog\/2014\/07\/14\/some-fun-with-iterators\/\">Some fun with iterators<\/a>.<\/p>\n<p>Unfortunately it&#8217;s not that simple. There is one data structure that is traversable but does not implement <code>Traversable<\/code>: PHPs main collection type <code>array<\/code>. This means that traversables cannot be given to a function with an <code>array<\/code> type hint, and arrays cannot be given to a function with a <code>Traversable<\/code> type hint.<\/p>\n<pre class=\"lang:php decode:true\">function haveSomeStrings( array $inputValues ) {\n    foreach ( $inputValues as $value ) {\n        echo 'Wow. Such string. Many omg';\n    }\n}\n\nhaveSomeStrings( $stringTraversable ); \/\/ Boom!<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1633\" data-permalink=\"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/dfp\/\" data-orig-file=\"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg\" data-orig-size=\"723,547\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"dfp\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg\" class=\"alignnone wp-image-1633 \" src=\"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg\" alt=\"Double facepalm\" width=\"348\" height=\"263\" \/><\/p>\n<p>This is really silly, as a lot of these functions work with both, without trying, if not for the type hint. The type hint can of course be dropped, though then again we end up with the documentation downside, and also run into the problem that the documentation cannot specify &#8220;Traversable of type&#8221;.<\/p>\n<pre class=\"lang:php decode:true \">\/**\n * @param string[]|Traversable $inputValues\n *\/\nfunction haveSomeStrings( $inputValues ) {\n    foreach ( $inputValues as $value ) {\n        echo 'Wow. Such string. Many omg';\n    }\n}<\/pre>\n<p>I ranted about this before in my <a href=\"https:\/\/www.entropywins.wtf\/blog\/2015\/08\/19\/lazy-iterators-in-php-and-python\/\">Lazy iterators in PHP and Python post<\/a>.<\/p>\n<p>Edit: I&#8217;m very happy that PHP 7.1 has fixed this issue by introducing the <a href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/12\/02\/php-7-1-is-awesome\/\">iterable pseudo-type<\/a>.<\/p>\n<p><strong>Additional thoughts<\/strong><\/p>\n<p>In this post I&#8217;ve focused on type hinting, as I felt\u00a0this is most in line with the goals of <a href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/01\/30\/missing-in-php7\/\">this blog post series<\/a>, and that these problems are both less known and less written about than other Collection features PHP misses. Things such as generics and a better PHP standard collection library are of course very welcome.<\/p>\n<p>Not everyone agrees that type hinting is a wise practice. My personal opinion is that it is generally good, and that it&#8217;s extremely bad to not be able to do it at all. This blog post is written from that perspective. If you have a different opinion, feel free to shout at me in a comment.<\/p>\n<p><strong>See also<\/strong><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><a href=\"https:\/\/github.com\/morrisonlevi\/Ardent\">Ardent<\/a> &#8211; A Collections library for PHP<\/li>\n<li><a href=\"https:\/\/github.com\/JeroenDeDauw\/BatchingIterator\">Batching Iterator<\/a> &#8211; <span class=\"repository-meta-content\">Iterator that fetches values in batch<\/span><\/li>\n<li><a href=\"https:\/\/github.com\/JeroenDeDauw\/RewindableGenerator\">Rewindable Generator<\/a> &#8211; <span class=\"repository-meta-content\">Simple adapter to make generators rewindable<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This is the fourth post in my Missing in PHP7 series. The previous one is about Value Objects. In this&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[328,408,179,195,441,197,316,409],"class_list":["post-1629","post","type-post","status-publish","format-standard","hentry","category-programming","tag-clean-code","tag-collections","tag-oop","tag-php","tag-php7","tag-planet-wikimedia","tag-software-design","tag-type-hinting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Missing in PHP7: Collections - Blog of Jeroen De Dauw<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Missing in PHP7: Collections - Blog of Jeroen De Dauw\" \/>\n<meta property=\"og:description\" content=\"This is the fourth post in my Missing in PHP7 series. The previous one is about Value Objects. In this&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog of Jeroen De Dauw\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-22T09:57:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-07-31T12:03:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg\" \/>\n<meta name=\"author\" content=\"Jeroen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/JeroenDeDauw\" \/>\n<meta name=\"twitter:site\" content=\"@JeroenDeDauw\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeroen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/\"},\"author\":{\"name\":\"Jeroen\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\"},\"headline\":\"Missing in PHP7: Collections\",\"datePublished\":\"2016-02-22T09:57:13+00:00\",\"dateModified\":\"2018-07-31T12:03:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/\"},\"wordCount\":716,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\"},\"image\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/02\\\/dfp.jpg\",\"keywords\":[\"Clean Code\",\"Collections\",\"OOP\",\"PHP\",\"PHP7\",\"Planet Wikimedia\",\"Software design\",\"Type hinting\"],\"articleSection\":[\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/\",\"url\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/\",\"name\":\"Missing in PHP7: Collections - Blog of Jeroen De Dauw\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/02\\\/dfp.jpg\",\"datePublished\":\"2016-02-22T09:57:13+00:00\",\"dateModified\":\"2018-07-31T12:03:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/02\\\/dfp.jpg\",\"contentUrl\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/02\\\/dfp.jpg\",\"width\":723,\"height\":547},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/2016\\\/02\\\/22\\\/missing-in-php7-collections\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Missing in PHP7: Collections\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/\",\"name\":\"Entropy Wins\",\"description\":\"A blog on Software Architecture, Design and Craftsmanship\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.entropywins.wtf\\\/blog\\\/#\\\/schema\\\/person\\\/4e2ef14f2ca7dc3a0ac137d1692b66b7\",\"name\":\"Jeroen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\",\"caption\":\"Jeroen\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/jeroendedauw\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/JeroenDeDauw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Missing in PHP7: Collections - Blog of Jeroen De Dauw","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/","og_locale":"en_US","og_type":"article","og_title":"Missing in PHP7: Collections - Blog of Jeroen De Dauw","og_description":"This is the fourth post in my Missing in PHP7 series. The previous one is about Value Objects. In this&hellip;","og_url":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/","og_site_name":"Blog of Jeroen De Dauw","article_published_time":"2016-02-22T09:57:13+00:00","article_modified_time":"2018-07-31T12:03:02+00:00","og_image":[{"url":"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg","type":"","width":"","height":""}],"author":"Jeroen","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/JeroenDeDauw","twitter_site":"@JeroenDeDauw","twitter_misc":{"Written by":"Jeroen","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#article","isPartOf":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/"},"author":{"name":"Jeroen","@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7"},"headline":"Missing in PHP7: Collections","datePublished":"2016-02-22T09:57:13+00:00","dateModified":"2018-07-31T12:03:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/"},"wordCount":716,"commentCount":2,"publisher":{"@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7"},"image":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#primaryimage"},"thumbnailUrl":"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg","keywords":["Clean Code","Collections","OOP","PHP","PHP7","Planet Wikimedia","Software design","Type hinting"],"articleSection":["Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/","url":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/","name":"Missing in PHP7: Collections - Blog of Jeroen De Dauw","isPartOf":{"@id":"https:\/\/www.entropywins.wtf\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#primaryimage"},"image":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#primaryimage"},"thumbnailUrl":"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg","datePublished":"2016-02-22T09:57:13+00:00","dateModified":"2018-07-31T12:03:02+00:00","breadcrumb":{"@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#primaryimage","url":"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg","contentUrl":"https:\/\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/dfp.jpg","width":723,"height":547},{"@type":"BreadcrumbList","@id":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/22\/missing-in-php7-collections\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.entropywins.wtf\/blog\/"},{"@type":"ListItem","position":2,"name":"Missing in PHP7: Collections"}]},{"@type":"WebSite","@id":"https:\/\/www.entropywins.wtf\/blog\/#website","url":"https:\/\/www.entropywins.wtf\/blog\/","name":"Entropy Wins","description":"A blog on Software Architecture, Design and Craftsmanship","publisher":{"@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.entropywins.wtf\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.entropywins.wtf\/blog\/#\/schema\/person\/4e2ef14f2ca7dc3a0ac137d1692b66b7","name":"Jeroen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g","caption":"Jeroen"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d62e6b5b8e332335cf17854fac850d9c70ba367c4692872613c3110ebd4e009b?s=96&d=mm&r=g"},"sameAs":["https:\/\/www.linkedin.com\/in\/jeroendedauw\/","https:\/\/x.com\/https:\/\/twitter.com\/JeroenDeDauw"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p74TBF-qh","jetpack-related-posts":[{"id":1577,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/01\/30\/missing-in-php7-function-references\/","url_meta":{"origin":1629,"position":0},"title":"Missing in PHP7: function references","author":"Jeroen","date":"2016-01-30","format":false,"excerpt":"This is the first post in my Missing in PHP7 series. Over time, PHP has improved its capabilities with regards to functions.\u00a0As of PHP 5.3 you can create anonymous functions and as of 5.4 you can use the callable type hint. However referencing a function still requires using a string.\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1642,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/03\/05\/missing-in-php7-nullable-return-types\/","url_meta":{"origin":1629,"position":1},"title":"Missing in PHP7: Nullable return types","author":"Jeroen","date":"2016-03-05","format":false,"excerpt":"This is the fifth post in my Missing in PHP7 series. The previous one is about Collections. This is a short post, as there really is not much to explain or motivate. Nullable return types As of PHP7, we have return types at our disposal. Unfortunately, these have some annoying\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1898,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/12\/02\/php-7-1-is-awesome\/","url_meta":{"origin":1629,"position":2},"title":"PHP 7.1 is awesome","author":"Jeroen","date":"2016-12-02","format":false,"excerpt":"PHP 7.1 has been released, bringing some features I was eagerly anticipating and some surprises that had gone under my radar. New iterable pseudo-type This is the feature I'm most exited about, perhaps because I had no clue it was in the works. In short, iterable allows for type hinting\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1548,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/01\/30\/missing-in-php7\/","url_meta":{"origin":1629,"position":3},"title":"Missing in PHP7","author":"Jeroen","date":"2016-01-30","format":false,"excerpt":"I've decided to start a series of short blog posts on how PHP gets in the way of\u00a0creating of well designed applications, with a focus on missing features. The language flamewar PHP is one of those languages that people love to hate. Its standard library is widely inconsistent, and its\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"PHP7","src":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/01\/PHP7.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2848,"url":"https:\/\/www.entropywins.wtf\/blog\/2022\/01\/30\/value-objects-with-php-8-1\/","url_meta":{"origin":1629,"position":4},"title":"Value Objects with PHP 8.1","author":"Jeroen","date":"2022-01-30","format":false,"excerpt":"PHP has introduced a number of new features that allow you to write awesome Value Objects with PHP 8.1. In this post you will learn about each feature and how to put them all together. Back in the dark ages of 2016, shortly after PHP 7.0 was released, I wrote\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/i-will-always-favor-value-objects.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/i-will-always-favor-value-objects.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/i-will-always-favor-value-objects.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1607,"url":"https:\/\/www.entropywins.wtf\/blog\/2016\/02\/03\/missing-in-php7-value-objects\/","url_meta":{"origin":1629,"position":5},"title":"Missing in PHP7: Value objects","author":"Jeroen","date":"2016-02-03","format":false,"excerpt":"This is the third post in my Missing in PHP7 series. The previous one is about\u00a0named parameters. A Value Object does not have an identity, which means that if you have two of them with the same data, they are considered equal (take two latitude, longitude pairs for instance). Generally\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.entropywins.wtf\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/i-will-always-favor-value-objects.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/i-will-always-favor-value-objects.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.entropywins.wtf\/blog\/wp-content\/uploads\/2016\/02\/i-will-always-favor-value-objects.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts\/1629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/comments?post=1629"}],"version-history":[{"count":11,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions"}],"predecessor-version":[{"id":2263,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/posts\/1629\/revisions\/2263"}],"wp:attachment":[{"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/media?parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/categories?post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.entropywins.wtf\/blog\/wp-json\/wp\/v2\/tags?post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}