PHP Tools includes support for the new PHP 7.4, including:
PHP Tools supports installing PHP 7.4 right from within Visual Studio. From within a Visual Studio PHP project:
Properties
).Debug -> Run
or select F5
and PHP Tools detects if PHP 7.4 and Xdebug for PHP 7.4 is installed. If not, it prompts the user to install it.To keep up with the PHP 7.4 syntax, PHP Tools has been updated to handle all syntax updates. Some of these updates:
fn
command)Arrow functions, otherwise known as anonymous functions, allow developers to quickly define a function, the parameters, and the return value in one line. For example:
$squarethis = 9;
$squared = fn($square) => $square * $square;
echo "<p>Number to be squared: $squarethis.</p>";
echo "<p>Squared number: ." $squared($squarethis).".</p>";
PHP, like other scripted languages, has supported the ability to have variables which its data type (integer, string, etc) was derived based on the context. So setting a variable from $i = 5;
and then $i = "5Bob";
are equally valid.
PHP 7.4 allows more control with scalar types and return values, allowing the user to specify exactly what kind of data they are receiving and sending back. PHP Tools is able to parse this syntax, and even return errors when detected. For example:
$squarethis = 9;
$squarebad = "Test";
function SquareValue(int $newNumber) : int
{
return $newNumber * $newNumber;
}
echo "<p>Using SquareValue function: " . SquareValue($squarebad) . ".</p>";
PHP Tools ability to resolve typed properties for variables and functions extends to arrays as well, and will issue warnings when arrays are expected and a different typed value is submitted:
/**
* Returns an array from $countdown down to 0.
* @param int $countdown
* @return array
*/
function ReturnArrayofValues(int $countdown) : array
{
$list = array();
for (; $countdown >= 0; $countdown--)
{
$list[] = $countdown;
}
return $list;
}
/**
* Prints an array of integer values in order
* @param array $list
*/
function PrintArrayofValues(array $list)
{
echo "<p>";
foreach($list as $item)
{
echo "<br>$item";
}
echo "</p>";
}
PrintArrayofValues(ReturnArrayofValues(10));
A new feature in PHP 7.4 is spread arrays, similar to how they work in JavaScript. For example, previously arrays would have to be copied and expanded like so:
$a = array(1, 2, 3);
$b = $a;
$b[] = 4;
$b[] = 5;
$b[] = 6;
print_r($b);
//output:
//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
The new spread method allows users to easily copy arrays into other arrays, or reference entire array elements at once:
$c = array(1, 2, 3);
$d = [...$c, 4, 5, 6];
print_r($d);
//output:
//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
With PHP 7.4, the new standard for serialization is based on two new magic functions that developers can define within their class:
__serialize
: Returns an array value that can then be parsed into a text string.__unserialize
: Takes the serialized array object and assigns the class members value from that array.With PHP Tools, developers are provided with helpful information processing these new magic functions. For example:
PHP 7.4 has depreciated a number of functions. Rather than guess what functions have been depreciated, PHP Tools lets you know which are still valid, and which need to go from your code:. Items that have been tagged with @depreciated
will have an error display in the IDE, and will show that status on on a mouse-over: