PHP Internals News: Episode 67: Match Expression – Derick Rethans

PHP Internals News: Episode 67: Match Expression

In this episode of “PHP Internals News” I chat with Derick Rethans (Twitter, GitHub, Website) about the new Match Expression in PHP 8.

The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode’s MP3 file, and it’s available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news

Transcript

Derick Rethans 0:15

Hi, I’m Derick, and this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 67. Today we’re going to talk about a match expression. I have asked the author of the match expression RFC, lija Tovilo, whether it wanted to come and speak to me about the match expression, but he declined. As I think it’s important that we talk in some depth about all the new features in PHP eight, I decided to interview myself. This is probably going to sound absolutely stupid, but I thought I’d give it a go regardless. So here we go.

Derick Rethans 0:53

Hi Derick, would you please introduce yourself?

Derick Rethans 0:56

Hello, I’m Derick and I’m the author of Xdebug I’m also PHP seven four’s release manager. I’m also the host of this podcast. I’m also you.

Derick Rethans 1:07

What a coincidence!

Derick Rethans 1:10

So what is the problem that is RFC is trying to solve?

Derick Rethans 1:13

Well, before we talk about the match expression, we really need to talk about switch. Switch is a language construct in PHP that you probably know, allows you to jump to different cases depending on the value. So you have to switch statement: switch parentheses open, variable name, parenthesis close. And then for each of the things that you want to match against your use: case condition, and that condition can be either static value or an expression. But switch has a bunch of different issues that are not always great. So the first thing is that it matches with the equals operator or the equals, equals signs. And this operator as you probably know, will ignore types, causing interesting issues sometimes when you’re doing matching with variables that contain strings with cases that contains numbers, or a combination of numbers and strings. So, if you do switch on the string foo. And one of the cases has case zero, and it will still be matched because we put type equal to zero, and that is of course not particularly useful. At the end of every case statement you need to use break, otherwise it falls down to the case that follows. Now sometimes that is something that you want to do, but in many other cases that is something that you don’t want to do and you need to always use break. If you forget, then some weird things will happen sometimes. It’s not a common thing to use it switch is that we switch on the variable. And then, what you really want to do the result of, depending on which case is being matched, assign a value to a variable and the current way how you need to do that now is case, say case zero result equals string one, break, and you have case two where you don’t set return value equals string two and so on and so on, which isn’t always a very nice way of doing it because you keep repeating the assignment, all the time. And another but minor issue with switch is that it is okay not to cover every value with a condition. So it’s totally okay to have case statements, and then not have a condition for a specific type and switch doesn’t require you to add default at the end either, so you can actually have having a condition that would never match any case, and you have no idea that that would happen.

Derick Rethans 3:34

Truncated by Planet PHP, read more at the original (another 12586 bytes)

Byte-sized functional programming: Composition over inheritance for functions, too – larry@garfieldtech.com

Byte-sized functional programming: Composition over inheritance for functions, too

A popular refrain in object-oriented code is to favor object composition over inheritance. It offers more flexibility, less overhead, and ends up being easier to reason about. The same concept applies to functions, too!

A common pattern is to have a function that does some work and then calls another function, which does some work and calls another function, and so on. The problem is that the first function then cannot be used or tested without the entire chain of other functions it calls. This is the function equivalent of “inheritance.”

Instead, we can compose functions, that is, pipe them together. Instead, take the output of the first function and pass it to the second, then take the second’s output and pass it to the third, etc. That way, each of the functions can be reused, tested, and understood in isolation, then we can stick them together like LEGO blocks to build whatever series of steps we want.

That is, instead of this:

<?php
function A($in)
{
  
// ...
  
return B($out);
}

function

B($in)
{
  
// ...
  
return C($out);
}

function

C($in)
{
  
// ...
  
return $out;
}
?>

Structure it like this:

<?php
function A($in)
{
   
// ...
   
return $out;
}

function

B($in)
{
   
// ...
   
return $out;
}

function

C($in)
{
   
// ...
   
return $out;
}

function

doit($in) {
   
$out = A($in);
   
$out = B($out);
   
$out = C($out);
    return
$out;
}
?>

Now `A()`, `B()`, and `C()` are all easier to read, understand, and test, and we can more easily add a step B2 or D if we want. So powerful is this concept that many languages have a native operator for piping functions together like that. PHP doesn’t, yet, but it’s straightforward enough to do in user space anyway.


Want to know more about functional programming and PHP? Read the whole book on the topic: Thinking Functionally in PHP.

Thinking Functionally in PHP

Larry
15 August 2020 – 10:07am

Recursive PHP lint – Rob Allen

There are many scripts that recursively execute php -l on a set of files or directories. This is mine:

#!/usr/bin/env bash
set -o nounset # Recursively call `php -l` over the specified directories/files if [ -z "$1" ] ; then printf 'Usage: %s  ...\n' "$(basename "$0")" exit 1
fi ERROR=false
SAVEIFS=$IFS
IFS=$'\n'
while test $# -gt 0; do CURRENT=${1%/} shift if [ ! -f $CURRENT ] && [ ! -d $CURRENT ] ; then echo "$CURRENT cannot be found" ERROR=true continue fi for FILE in $(find $CURRENT -type f -name "*.php") ; do OUTPUT=$(php -l "$FILE" 2> /dev/null) # Remove blank lines from the `php -l` output OUTPUT=$(echo -e "$OUTPUT" | awk 'NF') if [ "$OUTPUT" != "No syntax errors detected in $FILE" ] ; then echo -e "$FILE:" echo -e " ${OUTPUT//$'\n'/\\n }\n" ERROR=true fi done
done IFS=$SAVEIFS if [ "$ERROR" = true ] ; then exit 1
fi echo "No syntax errors found."
exit 0

I store it in ~/bin and usually run it like this:

$ cd project
$ phplint .
No syntax errors found.

There are a few interesting bash tricks that I picked up when I wrote this.

Firstly, you need to set IFS to break on new line rather than space otherwise the find command doesn’t work with spaces in file names.

I also discovered that the output of php -l has quite a lot of blank lines in its output that I didn’t want. OUTPUT=$(echo -e "$OUTPUT" | awk 'NF') solves this nicely.

I also wanted to indent the output and used bash’s parameter expansion system to replace a new line with a new line and two spaces using ${OUTPUT//$'\n'/\\n }

Maybe you’ll find this useful or it’ll work as the basis for a script you need to write.

Eighers Gonna Eight with Sara Golemon and Garbriel Caruso – Voices of the ElePHPant

This episode is sponsored by
Ring Central Developers

The post Eighers Gonna Eight with Sara Golemon and Garbriel Caruso appeared first on Voices of the ElePHPant.

Xdebug Update: July 2020 – Derick Rethans

Xdebug Update: July 2020

Another monthly update where I explain what happened with Xdebug development in this past month. These will be published on the first Tuesday after the 5th of each month. Patreon and GitHub supporters will get it earlier, on the first of each month. You can become a patron to support my work on Xdebug. If you are leading a team or company, then it is also possible to support Xdebug through a subscription.

In July, I worked on Xdebug for about 100 hours, with funding being around 70 hours. I worked mostly on the following things:

Xdebug 3

I spend nearly all of my time improving performance this month, with some help by Michael Voříšek for the profiler feature. Some of this work I have done live through Twitch where I stream (almost) every Monday at 15:30 BST (14:30 UTC/10:30 EDT). Past sessions are available on Vimeo.

In order to find out what can be improved I profiled Xdebug running various workloads. I have selected the following workloads per Xdebug mode

If you have any specific one you’d like to see added, please let me know and I’ll see whether I have CPU cycles for it—running composer update for Pimcore under the C profiler Valgrind takes 2½ hours per run!

In any case, the profiling found out the following possible improvements:

Switch xdebug_sprintf to a new xdebug_str_add_fmt

Xdebug often needs to convert data into a single string, for either writing to file or network, storing function names with arguments in memory, or display purposes. It uses an API xdebug_sprintf which allocates new memory and formats the string according to format specifiers. Often it needs to add this to a xdebug_str buffer that makes up a longer piece of text. After it has added it to that buffer, it frees the allocated memory again.

By creating a new API that can add a formatted string to an xdebug_str buffer, I managed to reduce the amount of memory allocations and frees dramatically. This gave a 10-15% Wall Time performance boost, and a 10-25% reduction in CPU instructions.

Truncated by Planet PHP, read more at the original (another 3405 bytes)

PHP Internals News: Episode 65: Null safe operator – Derick Rethans

PHP Internals News: Episode 65: Null safe operator

In this episode of “PHP Internals News” I chat with Dan Ackroyd (Twitter, GitHub) about the Null Safe Operator RFC.

The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode’s MP3 file, and it’s available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news

Transcript

Derick Rethans 0:18

Hi, I’m Derick, and this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 65. Today I’m talking with Dan Ackroyd about an RFC that he’s been working on together with Ilija Tovilo. Hello, Dan, would you please introduce yourself?

Dan Ackroyd 0:37

Hi Derick, my name is Daniel, I’m the maintainer of the imagick extension, and I occasionally help other people with RFCs.

Derick Rethans 0:45

And in this case, you helped out Ilija with the null safe operator RFC.

Dan Ackroyd 0:50

It’s an idea that’s been raised on internals before but has never had a very strong RFC written for it. Ilija did the technical implementation, and I helped him write the words for the RFC to persuade other people that it was a good idea.

Derick Rethans 1:04

Ilija declined to be talking to me.

Dan Ackroyd 1:06

He sounds very wise.

Derick Rethans 1:08

Let’s have a chat about this RFC. What is the null safe operator?

Dan Ackroyd 1:13

Imagine you’ve got a variable that’s either going to be an object or it could be null. The variable is an object, you’re going to want to call a method on it, which obviously if it’s null, then you can’t call a method on it, because it gives an error. Instead, what the null safe operator allows you to do is to handle those two different cases in a single line, rather than having to wrap everything with if statements to handle the possibility that it’s just null. The way it does this is through a thing called short circuiting, so instead of evaluating whole expression. As soon as use the null safe operator, and when the left hand side of the operator is null, everything can get short circuited, or just evaluates to null instead.

Derick Rethans 1:53

So it is a way of being able to call a methods. A null variable that can also represent an object and then not crash out with a fatal error

Dan Ackroyd 2:02

That’s what you want is, if the variable is null, it does nothing. If a variable was the object, it calls method. This one of the cases where there’s only two sensible things to do, having to write code to handle the two individual cases all the time just gets a bit tedious to write the same code all the time.

Derick Rethans 2:20

Especially when you have lots of nested calls I suppose.

Dan Ackroyd 2:25

That’s right. It doesn’t happen too often with code, but sometimes when you’re using somebody else’s API, where you’re getting structured data back like in an in a tree, it’s quite possible that you have the first object that might be null, it’s not null, it’s going to point to another object, and the object could be null so and so so down the tree of the structure of the data. It gets quite tedious, just wrapping each of those possible null variables with a if not null.

Derick Rethans 2:55

The RFC as an interesting ex

Truncated by Planet PHP, read more at the original (another 22729 bytes)