php en A look at the splat operator (...) <div data-history-node-id="14" class="node node--type-blog-post node--view-mode-rss ds-1col clearfix"> <div class="field field--name-dynamic-token-fieldnode-custom-submitted field--type-ds field--label-hidden field__item"><p>Submitted by <a href="">Andrej Galuf</a> on 17. April 2017.</p> </div> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>Today we are going to look at something that's been missing from PHP for far too long - the ... token, a.k.a. <strong>splat operator</strong>. The splat operator helps us to convert an array of values to function's arguments.</p> <p>Let's create a simple class with a method that takes three arguments:</p> <pre> <code class="language-php">&lt;?php class Example { /** * An example function with three integer arguments */ public static function setColor($red, $green, $blue) { // Do something here. } }</code></pre> <p>Now, let's suppose we have an array of three colors: $colors = [255, 0, 0];</p> <p>How would we pass these values to our method? Well, the most direct approach is to simply pass the values to it directly:</p> <pre> <code class="language-php">&lt;?php Example::setColor($color[0], $color[1], $color[2]);</code></pre> <p>Now, I don't know about you, but I'm not here to manually set all the array values - after all, we have a computer to do that for us and this gets real old real fast. Surely, there must be a better option? Well, there is... kind of. We can call a function and pass arguments to it as an array:</p> <pre> <code class="language-php">&lt;?php call_user_func_array(array('Example', 'setColor'), $colors);</code></pre> <p>Ok, that's a bit better, but it's not pretty and the performance is not exactly stellar either. Do we have another option?</p> <p>Since PHP 5.6, the answer is yet. We have a new <strong>splat operator</strong> (...) available that simplifies things for us. What the splat operator does is it takes an array that's been passed to the function as an argument and splits it up into individual argument values. Let's see that in action:</p> <pre> <code class="language-php">&lt;?php Example::setColor(...$colors);</code></pre> <p>Yup, those three dots in front of our array are it. An array passed into the method will automatically be converted to a set of arguments, and what do you know - the performance is up to 5x better than call_user_func_array() in PHP 5.6.</p> <p>So there you have it, the splat operator. It's awesome, it has an awesome (unofficial) name, use it.</p> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tags/php" hreflang="en">php</a></div> </div> </div> <div class="field field--name-dynamic-token-fieldnode-comments-title field--type-ds field--label-hidden field__item"><span>Comments</span> </div> <section class="field field--name-comment field--type-comment field--label-hidden comment-wrapper"> <h2 class="title comment-form__title">Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=14&amp;2=comment&amp;3=comment" token="fmIfbiR8nl0e3Twra6AbI8B26q-U9xssL7b2YNbxsOs"></drupal-render-placeholder> </section> </div> Mon, 17 Apr 2017 15:58:51 +0000 admin 14 at