Destructuring in php?

I work on javascript/typescript on a constant basis, and php on an infrequent basis (on my legacy php app http://www.ninjawars.net).

When I switch back to php after a long time of using javascript or typescript, I really miss the clean code that you c…


This content originally appeared on DEV Community and was authored by Roy Ronalds

I work on javascript/typescript on a constant basis, and php on an infrequent basis (on my legacy php app http://www.ninjawars.net).

When I switch back to php after a long time of using javascript or typescript, I really miss the clean code that you can write by using destructuring.

Now, I talked a bit in another post about short circuit evaluation in js vs php, and that terseness of syntax allows for writing code with much less boilerplate, but what about destructuring in php?

Destructuring in js

First, let's glance at destructuring in js:

const {a, b, c, ...rest} = {a:'Apple', b:'Banana', c:'Cantalope', d:'Durian'}

...not only is destructuring smooth, it also allows for the powerful ...rest spread syntax. I can't tell you the number of times that I have had some complex code block in javascript that was simplified by converting it to use destructuring, or a possibly undefined property that destructuring handles smoothly, like:
property api.data.user.name may be undefined

// The dreaded chain of && to avoid undefined properties
if(api && api.data && api.data.user && api.data.user.name){
 const username = api.data.user.name || 'unknown' // finally do something
}

another approach using the optional chaining operator from typescript:

const username = api?.data?.user?.name || 'Unknown user'

But here's another option, using destructuring:

const { data={} } = api
const { user={} } = data
const { name='Unknown' } = user
console.log(name) // Unknown

Destructuring in php

Sadly, php only supports a very limited bit of destructuring, specifically, when destructuring arrays, by using list() destructuring or [] destructuring.

[$a, $b, $c] = ['apple', 'banana', 'cantaloupe'];

OR

[,,,$d,$e] = ['apple', 'banana', 'cantaloupe', 'durian', 'eggplant'];

Thankfully, you are also allowed to destructure named keys from associative arrays:

$fruit = [
 'a'=>'apple',
 'b'=>'banana',
 'c'=>'cantaloupe', 
 'd'=>'durian',
 'e'=>'eggplant'
];
// Have to set the $fruit var because for some reason php doesn't like it when you try to directly destructure from the  associative array you just created.
['d'=>$d, 'e'=>e] = $fruit;
echo $d; // Durian now
echo $e; // Eggplant

So as long as you are dealing with an array or an associative array you can do some destructuring.

Destructuring php objects

It's somewhat possible to destructure a php object by transforming it into an array.

$api = {
  data: {
    user: {
      name: 'George Washington'
  }
}; // A nested data object

To destructure this you generally have to turn it into an array first, and deal with the proposition of turning it's nested properties into arrays as well. This may be a diminishing return on effort.

What about ...spreading values?

More modern versions of php, at least 8+, support spread/splat operators on arrays in weird ways, having supported spread operators on function arguments since way back in php 5.6.

Spread on arrays in php:

$fruit = ['apple', 'banana', 'canteloupe', 'durian'];
[$a, ...$rest] = $fruit;
var_dump($rest); // ['banana', 'canteloupe', 'durian']

For whatever reason, the above doesn't quite work, php errors out saying:
PHP Fatal error: Spread operator is not supported in assignments in php shell code on line 1

So the spread operator exists, but it doesn't work quite the way I would expect in php.

The takeaway

Still, there is a lot of potential for destructuring as a way to clean up and simplify code.


This content originally appeared on DEV Community and was authored by Roy Ronalds


Print Share Comment Cite Upload Translate Updates
APA

Roy Ronalds | Sciencx (2021-10-31T17:14:36+00:00) Destructuring in php?. Retrieved from https://www.scien.cx/2021/10/31/destructuring-in-php/

MLA
" » Destructuring in php?." Roy Ronalds | Sciencx - Sunday October 31, 2021, https://www.scien.cx/2021/10/31/destructuring-in-php/
HARVARD
Roy Ronalds | Sciencx Sunday October 31, 2021 » Destructuring in php?., viewed ,<https://www.scien.cx/2021/10/31/destructuring-in-php/>
VANCOUVER
Roy Ronalds | Sciencx - » Destructuring in php?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/31/destructuring-in-php/
CHICAGO
" » Destructuring in php?." Roy Ronalds | Sciencx - Accessed . https://www.scien.cx/2021/10/31/destructuring-in-php/
IEEE
" » Destructuring in php?." Roy Ronalds | Sciencx [Online]. Available: https://www.scien.cx/2021/10/31/destructuring-in-php/. [Accessed: ]
rf:citation
» Destructuring in php? | Roy Ronalds | Sciencx | https://www.scien.cx/2021/10/31/destructuring-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.