PHP 8.0.0beta2 Released! – PHP: Hypertext Preprocessor

The PHP team is pleased to announce the fifth testing release of PHP 8.0.0, Beta 2. This continues the PHP 8.0 release cycle, the rough outline of which is specified in the PHP Wiki. For source downloads of PHP 8.0.0 Beta 2 please visit the download page.Please carefully test this version and report any issues found in the bug reporting system.Please DO NOT use this version in production, it is an early test version. For more information on the new features and other changes, you can read the NEWS file, or the UPGRADING file for a complete list of upgrading notes. These files can also be found in the release archive. The next release will be Beta 3, planned for Sep 3 2020.The signatures for the release can be found in the manifest or on the QA site.Thank you for helping us make PHP better.

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.