06/21/2019
The June version of the "Latest in PHP" is out! Shared at our monthly TrianglePHP meeting, here are the things you need to know. If you'd like the raw slides or use this as a starting point for your own meetup, please visit our GitHub Repository.
7
branch)More Info:
http://php.net/supported-versions.php
7.1. - 7.3. have Security and Bug fixes
More Info:
http://php.net/ChangeLog-7.php
// before
function array_values_from_keys($arr, $keys) {
return array_map(function ($x) use ($arr) { return $arr[$x]; }, $keys);
}
// after
function array_values_from_keys($arr, $keys) {
return array_map(fn($x) => $arr[$x], $keys);
}
https://wiki.php.net/rfc/arrow_functions_v2
PHP has already supported argument unpacking (AKA spread operator) since 5.6. This RFC proposes to bring this feature to array expression.
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
https://wiki.php.net/rfc/spread_operator_for_array
A new operator would be added to the language: ?->
. Calling $obj?->foo(..)
behaves identically to $obj->foo(..)
if $obj
is not null
. If $obj
is null
, then it returns null
.
function f($o) {
return $o?->mayFail1()?->mayFail2()?->mayFail3()?->mayFail4();
}
https://wiki.php.net/rfc/nullsafe_calls
https://wiki.php.net/rfc/alternative-closure-use-syntax
// before...
$closure = function (
ArgumentType $argument1,
ArgumentType $argument2,
ArgumentType $argument3,
ArgumentType $argument4
) use (
$importVariable1,
&$importVariable2,
$importVariable3,
&$importVariable4
): ReturnType {
// ...
};
// after...
$closure = function (
ArgumentType $argument1,
ArgumentType $argument2,
ArgumentType $argument3,
ArgumentType $argument4
): ReturnType {
use $importVariable1, &$importVariable2;
use $importVariable3, &$importVariable4;
// ...
};
Calculate Levenshtein distance between two strings
$str1 = "apple";
$str2 = "appplee";
echo levenshtein($str1, $str2); // 2
https://www.php.net/manual/en/function.levenshtein.php
// input misspelled word
$input = 'carrrot';
// array of words to check against
$words = array('apple','pineapple','banana','orange',
'radish','carrot','pea','bean','potato');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest...
foreach ($words as $word) {
// calculate the distance between the input word, and the current word
$lev = levenshtein($input, $word);
// check for an exact match
if ($lev == 0) {
$closest = $word;
$shortest = 0;
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
---
Input word: carrrot
Did you mean: carrot?
Laracon VII July 24–25, New York, New York https://laracon.us
SymfonyLive Berlin 2019 September 24–27, Berlin, Germany http://berlin2019.live.symfony.com
Cascadia PHP September 19–21, Portland, OR https://cascadiaphp.com
US: Double Loop: TDD & BDD Done Right presented by Jessica Mauerhan June 20, 2019 at 06:00pm PDT
UK: The Dark Corners of the SPL presented by Omni Adams June 20, 2019 at 11:00am PDT
PHPCon Poland 2019 CfP Deadline: July 10, 2019 November 15–17, Szczyrk, Poland https://2019.phpcon.pl
PHP.Barcelona Conferenence 2019 CfP Deadline: September 30th, 2019 November 12–13, Barcelona, Spain https://php.barcelona
All Things Open Oct 13-15, 2019 http://allthingsopen.org
Early bird going on now for $99!
TDB Presented By YOU?!?!
Thursday, July 18, 6pm @ Atlantic BT
Need an Idea?
Who's hiring? Who's looking? What are you looking for?