{"id":27463,"date":"2017-03-27T13:58:01","date_gmt":"2017-03-27T11:58:01","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=27463"},"modified":"2017-03-27T13:58:01","modified_gmt":"2017-03-27T11:58:01","slug":"creating-strictly-typed-arrays-collections-php","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/27\/creating-strictly-typed-arrays-collections-php\/","title":{"rendered":"Creating Strictly Typed Arrays and Collections in PHP"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p><a href=\"https:\/\/www.sitepoint.com\/creating-strictly-typed-arrays-collections-php\/\">This SitePoint PHP blog post<\/a>\u00a0(read at <a href=\"http:\/\/planet-php.org\/\">Planet PHP<\/a> if the site is unavailable) brings to light a very useful feature available since PHP 5.6 &#8211; <a href=\"http:\/\/php.net\/manual\/en\/functions.arguments.php#functions.variable-arg-list.new\">ellipses in functional arguments<\/a>, which allows to define a variable number of arguments to a function.<\/p>\n<p>I&#8217;ve seen the mentions of ellipses a few times now, but I assumed that it was a PHP 7 feature. \u00a0Turns out PHP 5.6 users can enjoy it as well:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nfunction sum(...$numbers) {\r\n    $acc = 0;\r\n    foreach ($numbers as $n) {\r\n        $acc += $n; \r\n    }   \r\n    return $acc;\r\n}\r\necho sum(1, 2, 3, 4); \/\/ prints out 10\r\n<\/pre>\n<p>This is very useful, but, as SitePoint PHP blog most mentions, it can be made even more useful with type hinting of the arguments. \u00a0For example:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php \r\nclass Movie { private $dates = &#x5B;];\r\n    public function setAirDates(\\DateTimeImmutable ...$dates) { \r\n        $this-&gt;dates = $dates;\r\n    }   \r\n\r\n    public function getAirDates() {\r\n        return $this-&gt;dates;\r\n    }   \r\n}\r\n<\/pre>\n<p>The limitation is that you can only hint a single type for all the arguments. The workaround this is to use collection classes, which basically work as strictly typed arrays.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>This SitePoint PHP blog post\u00a0(read at Planet PHP if the site is unavailable) brings to light a very useful feature available since PHP 5.6 &#8211; ellipses in functional arguments, which allows to define a variable number of arguments to a function. I&#8217;ve seen the mentions of ellipses a few times now, but I assumed that &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/27\/creating-strictly-typed-arrays-collections-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating Strictly Typed Arrays and Collections in PHP<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Creating Strictly Typed Arrays and Collections in PHP #WebDev #PHP","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},"jetpack_post_was_ever_published":false,"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[38,1330],"keyring_services":[],"class_list":["post-27463","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-php","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":27105,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/11\/php-static-analysis-tool-discover-bugs-in-your-code-without-running-it\/","url_meta":{"origin":27463,"position":0},"title":"PHP Static Analysis Tool &#8211; discover bugs in your code without running it!","author":"Leonid Mamchenkov","date":"December 11, 2016","format":false,"excerpt":"Ond\u0159ej Mirtes shares the idea behind the creation of\u00a0PHPStan - a static analysis tool for PHP: Compiled languages need to know about the type of every variable, return type of every method etc. before the program runs. This is why the compiler needs to make sure that the program is\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28827,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/09\/30\/highlights-from-git-2-19-and-php-syntax\/","url_meta":{"origin":27463,"position":1},"title":"Highlights from Git 2.19 and PHP syntax","author":"Leonid Mamchenkov","date":"September 30, 2018","format":false,"excerpt":"GitHub blog covers the highlights from Git 2.19, which was recently released. One particular change that I want to highlight (which GitHub skipped in their highlights, but which exists in the Git release notes for this version) is the syntax pattern update for the PHP files. Let's have a look\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2018\/09\/git-range-diff-500x243.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":21799,"url":"https:\/\/mamchenkov.net\/wordpress\/2014\/05\/13\/phpng-dramatic-speedup-features-coming-in-php-6-release\/","url_meta":{"origin":27463,"position":2},"title":"PHPNG Dramatic Speedup Features Coming in PHP 6 Release","author":"Leonid Mamchenkov","date":"May 13, 2014","format":"link","excerpt":"PHPNG Dramatic Speedup Features Coming in PHP 6 Release Some highlights: The plot to kill Apache mod_php SAPI Type hinting for everything (function arguments, function return values, and variables) Asynchronous programming (using the new await keyword) Production ready standalone web server Multi-thread safe","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28850,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/10\/10\/bitmask-constant-arguments-in-php\/","url_meta":{"origin":27463,"position":3},"title":"Bitmask Constant Arguments in PHP","author":"Leonid Mamchenkov","date":"October 10, 2018","format":false,"excerpt":"Liam Hammett\u00a0(check many of his other excellent posts) has a nice explanation of the bitmask constant arguments in PHP.\u00a0 These are fairly common and most PHP developers have seen them.\u00a0 However, it's been a few occasions where I discovered that especially newer developers don't understand how this works. Bitmask arguments\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27037,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/02\/migrating-to-php-7\/","url_meta":{"origin":27463,"position":4},"title":"Migrating to PHP 7","author":"Leonid Mamchenkov","date":"December 2, 2016","format":false,"excerpt":"PHP 7.0.0 has been released for a year now. \u00a0I wasn't in a rush to migrate to it, but with all the cool features and performance optimization, it's definitely something I wanted to look into rather sooner than later. It turns out that I've done my first PHP 7 migration\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29219,"url":"https:\/\/mamchenkov.net\/wordpress\/2019\/01\/14\/new-in-php-7-4\/","url_meta":{"origin":27463,"position":5},"title":"New in PHP 7.4","author":"Leonid Mamchenkov","date":"January 14, 2019","format":false,"excerpt":"\"New in PHP 7.4\" is a quick and simple introduction into changes and new features of the upcoming PHP 7.4, which is planned for release in December of 2019. In brief: Preloading (parsed source code caching)Typed propertiesImproved type varianceFFI (Foreign Function Interface) - a way to run C code from\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=27463"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27463\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=27463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=27463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=27463"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=27463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}