Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sebastian De Deyne:
The List Function & Practical Uses of Array Destructuring in PHP
May 15, 2017 @ 10:26:37

Sebastian De Deyne has written up a post to his site spotlighting PHP's list function and showing how it can be used for "array destructuring" and how recent changes in PHP 7.1.x make it more useful.

PHP 7.1 introduced a new syntax for the list() function. I've never really seen too much list() calls in the wild, but it enables you to write some pretty neat stuff.

This post is a primer of list() and it's PHP 7.1 short notation, and an overview of some use cases I've been applying them to.

He starts with a basic introduction to the list function and how it assigns out variables based on an array. He then shows examples of the updates that came with PHP 7.1, allowing you to specify the key from an array to more selectively extract only the value you want. Three "exhibits" are then provided, showing actual use cases for this functionality: basicunpacking examples, creating tuples and handling multiple return values.

tagged: list function use array destructuring php71 functionality tutorial tuple returnvalue

Link: https://sebastiandedeyne.com/posts/2017/the-list-function-and-practical-uses-of-array-destructuring-in-php

TutsPlus.com:
Working With PHP Arrays in the Right Way
Apr 26, 2017 @ 11:57:09

If you're relatively new to the PHP language and are just getting your feet wet, the massive amounts of array functionality included in the language could be confusing. This is where this new article on the TutsPlus.com site comes in, showing you how to work with PHP arrays "the right way".

n this tutorial, I am going to make a list of common PHP array functions with examples of usage and best practices. Every PHP developer must know how to use them and how to combine array functions to make code readable and short.

Also, there is a presentation with given code examples, so you can download it from the related links and show it to your colleagues to build a stronger team.

He starts out with some of the basics around using arrays in PHP and then quickly moves into other topics:

  • shortening code with functions like list
  • using the filtering functions
  • walking through array values
  • joining arrays
  • generating arrays
  • sorting the contents of arrays

He ends the post with a look at combining array functions to make it simpler to do things like remove empty values or return just the top three values.

tagged: tutorial introduction array functionality language

Link: https://code.tutsplus.com/tutorials/working-with-php-arrays-in-the-right-way--cms-28606

Exakat Blog:
Moving from array to class
Apr 12, 2017 @ 11:18:42

In a new post to the Exakat blog there's a proposal to replace uses of arrays with classes to make scripts more efficient and handle resources better behind the scenes.

Ever since I started using PHP, arrays have always been my friend. They are versatile, they have a wide range of functions, and they are easy to use. I kept using them versions after versions, and even with PHP 7.2, I still rely on them a lot. Over the years, classes have also made their way into my toolset. They have a different usage : classes are for complex data structures, for business logic. Simple data structures get an array. Until we tried what seemed impossible : a moving from an array to a class.

He mentions some of the recent changes in PHP 7 that make the use of classes over arrays a bit more advantageous. He then gets into how to take advantage of these efficiency benefits in moving from arrays to classes. He uses an example from his own work in the Exacat engine, how he performed the replacement and a small caveat he found when working with functions requiring array input. He ends the post with some of the other benefits from making the move including performance enhancements, readability and reduced memory usage.

tagged: array class performance difference tutorial php7

Link: https://www.exakat.io/moving-from-array-to-class/

SitePoint PHP Blog:
Creating Strictly Typed Arrays and Collections in PHP
Mar 27, 2017 @ 12:45:06

On the SitePoint PHP blog there's a new post from Bert Ramakers showing you how to create strictly typed arrays and collections in PHP.

One of the language features announced back in PHP 5.6 was the addition of the ... token to denote that a function or method accepts a variable length of arguments.

Something I rarely see mentioned is that it’s possible to combine this feature with type hints to essentially create typed arrays.

He starts with an example of a class/method that only takes in a certain type of objects as a collection (using the "...") with a fatal thrown if anything else is given. He also shows how to do the same thing with scalar types and the "..." operator with a typed input. He does point out one problem with this approach, namely that if more complex input is required the single type just wouldn't work. His solution involves custom collection classes where the settings are in the collection and not passed directly into the method. This collection then contains some of the base functionality (like getting an average value from a set of floats) and can be enhanced with other typical interfaces to work like any other collection. He also presents another option: using value objects for validation of the input.

tagged: collection array strict typing tutorial operator

Link: https://www.sitepoint.com/creating-strictly-typed-arrays-collections-php/

Sameer Borate:
New features in PHP 7.1
Feb 13, 2017 @ 10:57:45

The PHP 7.1.x releases are some of the latest versions of the language. There's plenty of new features that came along with this new release. In this new post to his CodeDiesel blog Sameer Borate looks at some of these new features (including code snippets to illustrate).

The PHP development team announced PHP 7.1.0 on 01 Dec 2016. This release is the first point release in the 7.x series. There are a few features – like the void return type – which have been introduced. Below are a few new selected features in PHP 7.1.

In the post he covers:

  • void functions (return type)
  • nullable types
  • symmetric array destructuring
  • class constant visibility

For each, code samples are provided and some of the benefits (and limitations) that come along with them.

tagged: feature php71 void nullable array class constant summary

Link: http://www.codediesel.com/php/new-features-in-php-7-1/

QaFoo Blog:
Extracting Data Objects
Feb 10, 2017 @ 12:16:36

On the QaFoo blog they have a new post offering some advice on extracting functionality to data objects and reducing the complexity of your application's interfaces.

Extracting data objects from your code will make it easier to read and write, easier to test and more forward compatible. This post shows you the two most common cases where introducing a data object makes sense and how to do it.

The first case covers the extraction when a method ends up with too many parameters. We've all been there and remembering the correct order and values for each (not to mention optional vs required). By making use of value objects you can reduce that down to one or two parameters that act as self-contained "containers" for the same values. They illustrate with a refactor of "product" search criteria into a "ProductCriteria" object. The second example show a refactor away from using an array as an input value and providing a bit more structure with a "Checkout" value object instead.

The post ends with a helpful hint about migrating from one method to the other in legacy systems using a "shim" method to handle the new case right alongside the old one.

tagged: data object refactor simplicity parameters array

Link: https://qafoo.com/blog/096_refactoring_extract_data_objects.html

Blackfire.io Blog:
PHP 7 performance improvements (1/5): Packed arrays
Nov 17, 2016 @ 11:06:53

On the Blackfire.io blog a new tutorial has been posted by Julien Pauli looking at some of the features of PHP 7 and how they relate to the overall performance in this latest major version of the language. In this first post in the series Julien talks about packed arrays.

This blog series will show you what changed inside the Zend engine between PHP 5 and PHP 7 and will detail how you, as a developer, may effectively use the new internal optimizations. We are taking PHP 5.6 as a comparison basis.

[...] Packed arrays is the first great PHP 7 optimization. Packed arrays consume less memory and are a bit faster in many operations than traditional arrays.

He gets into the specifics of how the packed arrays work, mentioning the internal optimization the language does, requiring no intervention in user-land code. He shows the difference between the PHP 5.6 performance and PHP 7 using the Blackfire.io tool - a difference of about a 70% gain.

tagged: php7 blackfire performance packed array feature optimize

Link: https://blog.blackfire.io/php-7-performance-improvements-packed-arrays.html

Adam Wathan:
Cleaning Up Form Input with Transpose
Apr 15, 2016 @ 11:50:34

Adam Wathan has a post on his site showing you how to use "transpose" functionality to clean up form input, transforming a set of arrays from submitted data back into a better structure.

Transpose is an often overlooked list operation that I first noticed in Ruby. The goal of transpose is to rotate a multidimensional array, turning the rows into columns and the columns into rows.

In his example, he adding multiple "contacts" at once. He shows the difficulties with this, the form structure and the data returned. There's several formats that could be returned but none are the correct structure to push into a model (his examples are in Laravel there's a generic array_map example too) He shows how to update the Laravel collection handling using a custom macro, transpose, to handle the array_map functionality in a more self-contained way. He includes the code to make the macro work and an example of it in use to correctly format his incoming contact data into something he can use in his collection.

tagged: transpose laravel collection array data format arraymap

Link: http://adamwathan.me/2016/04/06/cleaning-up-form-input-with-transpose/

SitePoint PHP Blog:
Fun and Functional Programming in PHP with Macros
Apr 04, 2016 @ 10:13:37

The SitePoint PHP blog has a new tutorial posted from author Christopher Pitt continuing on his look at macros in PHP (part one is here). In this new tutorial he gets beyond the basic example he provided in part one and recreate some expressive syntax from Javascript and prefixing strings.

I was so excited about my previous article about PHP macros, that I thought it would be fun for us to explore the intersection of macros and functional programming. PHP is already full of functions, with object oriented patterns emerging relatively late in its life. Still, PHP’s functions can be cumbersome, especially when combined with variable scope rules…

[...] It’s not significantly more code [to append the prefix in PHP vs Javascript], but it isn’t as clear or idiomatic as the JavaScript alternative. I often miss JavaScript’s expressive, functional syntax, when I’m building PHP things. I want to try and win back some of that expressive syntax!

He starts with a quick install of the yay library used in the first part of the series. Instead of the manual prefixing from his first example, he creates a macro that uses the array_map handling to generate the necessary code once the pre-compiler has done its job. He then expands on this simpler solution and updates it to allow for the setting of the prefix string. It gets a little complex but he walks through each step of the way, explaining the code that's added and what it expands out to. The result is a map method that generates a bit of code that's eval-ed to handle the prefixing automatically.

tagged: macro series part2 tutorial array map prefix advanced precompile yay library

Link: http://www.sitepoint.com/functional-programming-in-php-with-macros/

SitePoint PHP Blog:
PHP Macros for Fun and Profit!
Mar 21, 2016 @ 13:47:17

On the SitePoint PHP blog they've posted another tutorial from Christopher Pitt, this time about macros in PHP, and how you can use the Yay library to add in custom pre-processed macros to your code.

I get really excited when developers feel empowered to create new tools, and even new languages with which to solve their problems. You see, many developers come to PHP from other languages. And many PHP developers can code in more than one language. Often there are things in those languages — small syntax sugars — that we appreciate and even miss when we’re building PHP things.

Adding these to a language, at a compiler level, is hard (or is it?). That is unless you built the compiler and/or know how they work. We’re not going to do anything that technical, but we’re still going to be empowered.

He starts off by describing the goal: a simple "range" macro that creates an array and fills it with integers. He helps you get the library installed and shows how to use it to pre-process a file and output the PHP version. He shows how to create the syntax for the macro in the format Yay is expecting for the array_slice shortcut. He also includes handling letting you slice out a portion of an array using the same notation. Finally he shows the resulting code after the pre-processing has happened and the macros have been resolved.

tagged: macro library yay tutorial range integer string array

Link: http://www.sitepoint.com/php-macros-for-fun-and-profit/