4
The latest PHP version is updated and iterated very quickly, and many friends are unable to learn. And because the document has a certain degree of lag, it is also a bit of a threshold to look at the full English RFC. So this article wants to teach you a quick and comprehensive understanding of new features.

The technique is very simple to say, look at the unit test in the PHP source code.

Because there are many scenarios to be considered in unit testing, the use cases are very comprehensive. Some things that are not detailed in the document can be clearly reflected in the unit test code.

Not much to say, start to work.

Download source code

git clone https://github.com/php/php-src

See unit test

The unit test of each extension is in ext/extension/tests, and the file ending with the suffix phpt
The unit test of the built-in function is at Zend/tests .

Take the arrow function as an example

In the Zend/tests/arrow_functions directory, take a look at the content in 001.phpt

--TEST--
Basic arrow function functionality check
--FILE--
<?php

$foo = fn() => 1;
var_dump($foo());

$foo = fn($x) => $x;
var_dump($foo(2));

$foo = fn($x, $y) => $x + $y;
var_dump($foo(1, 2));

// Closing over $var
$var = 4;
$foo = fn() => $var;
var_dump($foo());

// Not closing over $var, it's a parameter
$foo = fn($var) => $var;
var_dump($foo(5));

// Close over $var by-value, not by-reference
$var = 5;
$foo = fn() => ++$var;
var_dump($foo());
var_dump($var);

// Nested arrow functions closing over variable
$var = 6;
var_dump((fn() => fn() => $var)()());
var_dump((fn() => function() use($var) { return $var; })()());

?>
--EXPECT--
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(5)
int(6)
int(6)

Divided into three paragraphs:
In the first paragraph, the subject description of this test is --TEST-- .
The second paragraph, the PHP code --FILE-- this single test.
The third paragraph, the expected output of this single test, if the expected output is wrong, use --EXPECTF-- , otherwise use --EXPECT--

Basically all unit tests in the PHP source code are in this format.

The first single test use case is to test whether the basic use of arrow functions meets expectations. It is very simple and I will not elaborate on the example.

Take a look at other use cases of arrow functions, the second single test file:

--TEST--
Arrow functions implicit use must be throwing notices only upon actual use
--FILE--
<?php

$b = 1;

var_dump((fn() => $b + $c)());

?>
--EXPECTF--
Warning: Undefined variable $c in %s on line %d
int(1)

It's also very simple. The purpose is to tell us that when an arrow function is defined, an error will not be reported when an undefined variable is used, and an error will only be thrown when it is actually used. In other words:

<?php
$a = 1;
$func = fn() => $a + $b;

This will not report an error, and only the following code will throw an error.

<?php
$a = 1;
$func = fn() => $a + $b;
$func();

Such details don't seem to be mentioned in the RFC, and can only be noticed through the unit test code.

Interested students can take a look at the single test of a few new functions. After reading it, you will ensure that you have gained something and have a more comprehensive understanding of the new function and new grammar.


church
3.6k 声望67 粉丝