The new learning features of PHP
Recent projects have used PHP7, but feel that there are many new features that are not used. Just to summarize some of the new features that might be used. The previously used environment is php5.4, and all of them will have php5.5 and php5.6 features, and here's just a list of the features I think might be used in the project, mainly from the appendix to the PHP Manual.
Generators (PHP 5 >= 5.5.0, PHP 7)
Generators,generators provides a simpler way to implement iterators by adding the yield keyword, without the need to implement the iterator interface.
<?php
function xrange ($start, $limit, $step = 1) {for
($i = $start; $i <= $limit; $i = = $step) {
yield $i;
}
}
echo ' single digit odd numbers: ';
/* Note the stored in-memory array will never be created or returned/
foreach (xrange (1, 9, 2) as $number) {
echo "$number";
}
The above routines will output:
Single digit odd numbers:1 3 5 7 9
Click on the builder for more details
Add finally keyword (PHP 5 >= 5.5.0, PHP 7)
Try-catch now supports finally
foreach now supports list () (PHP 5 >= 5.5.0, PHP 7)
The foreach control structure now supports separating nested arrays into separate variables through the list () construct. For example:
<?php
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list ($a, $b)) {
echo "a: $a; B: $b \ n ";
}
? >
The above routines will output:
a:1; B:2
A:3; B:4
Array_column (PHP 5 >= 5.5.0, PHP 7)
array_column-returns a column specified in the array
Define constants using Expressions (PHP 5 >= 5.6.0, PHP 7)
In previous versions of PHP, you had to use static values to define constants, declare properties, and specify default values for function parameters. You can now define constants, declare properties, and set function parameter defaults by using numeric expressions that include numeric values, string literals, and other constants.
<?php
Const ONE = 1;
CONST TWO = one * 2;
Class C {
Const THREE = two + 1;
Const ONE_THIRD = one/self::three;
Const SENTENCE = ' The value of THREE is '. Self::three;
Public function f ($a = one + Self::three) {return
$a;
}
}
Echo (New C)->f (). \ n ";
echo c::sentence;
? >
The above routines will output:
4
The value of THREE is 3
You can now define constants of type array by using the Const keyword.
<?php
Const ARR = [' A ', ' B '];
Echo arr[0];
? >
The above routines will output:
A
Use... operator defines variable-length parameter functions (PHP 5 >= 5.6.0, PHP 7)
Now can not rely on Func_get_args (), use ... operator to implement variable-length parameter functions.
<?php
function f ($req, $opt = null, ... $params) {
//$params is an array containing the remaining parameters
printf (' $req:%d; $opt:%d; numb Er of params:%d '. ' \ n ",
$req, $opt, Count ($params));
}
f (1);
F (1, 2);
F (1, 2, 3);
F (1, 2, 3, 4);
F (1, 2, 3, 4, 5);
? >
The above routines will output:
$req: 1; $opt: 0; Number of params:0
$req: 1; $opt: 2; Number of params:0
$req: 1; $opt: 2; Number of Params:1
$req: 1; $opt: 2; Number of Params:2
$req: 1; $opt: 2; Number of Params:3
Use... Operator for parameter expansion (PHP 5 >= 5.6.0, PHP 7)
When calling a function, use the ... operator to expand an array and an Ergodic object into a function argument. In other programming languages, such as Ruby, this is called a concatenation operator.
<?php
function Add ($a, $b, $c) {return
$a + $b + $c;
}
$operators = [2, 3];
echo Add (1, ... $operators);
? >
The above routines will output:
6
Use function and use const (PHP 5 >= 5.6.0, PHP 7)
The use operator is extended to support the import of external functions and constants in the class. The corresponding structure is the use function and the use Const.
<?php
namespace Name\space {
const FOO =;
function f () {echo __function__. " \ n "; }
}
namespace {use
const name\space\foo;
Use function name\space\f;
echo FOO. " \ n ";
f ();
}
? >
The above routines will output:
42
Name\space\f
__debuginfo () (PHP 5 >= 5.6.0, PHP 7)
adding __debuginfo (), when using Var_dump () output objects, can be used to control the properties and values to be exported.
<?php
class C {
private $prop;
Public function __construct ($val) {
$this->prop = $val;
}
Public Function __debuginfo () {return
[
' propsquared ' => $this->prop * * 2,
];
}
}
Var_dump (new C);
>
The above routines will output:
Object (C) #1 (1) {
["propsquared"]=>
Int (1764)
}
Scalar type declaration (PHP 7)
There are two modes of scalar type declarations: coercion (default) and strict mode. You can now use the following type parameters, whether in mandatory or strict mode: string, Integer (int), floating-point number (float), and Boolean value (BOOL). They extend the other types introduced in PHP5: class names, interfaces, arrays, and callback types.
<?php
//Coercive mode
function sumofints (int ... $ints) {return
array_sum ($ints);
}
Var_dump (sumofints (2, ' 3 ', 4.1));
The above routines will output:
Int (9)
To use strict mode, a declare declaration instruction must be placed at the top of the file. This means that strictly declaring a scalar is based on file availability. This directive affects not only the type declaration of the parameter, but also the return value declaration of the function (see return value type declaration, built-in PHP function, and PHP function loaded in the extension)
return value type declaration (PHP 7)
PHP 7 adds support for the return type declaration. Similar to a parameter type declaration, a return type declaration indicates the type of the function return value. The available types are the same as those available in the parameter declaration.
<?php
function arrayssum (array ... $arrays): array {return
Array_map (function (array $array): int { C13/>return array_sum ($array);
}, $arrays);
}
Print_r (Arrayssum ([1,2,3], [4,5,6], [7,8,9]);
The above routines will output:
Array
(
[0] => 6
[1] => 15
[2] => 24
)
Null merge operator (PHP 7)
Because of the large number of simultaneous use of ternary expressions and isset () in everyday use, we have added the syntactic sugar of the null merge operator (??). If a variable exists and the value is not NULL, it returns its own value, otherwise it returns its second operand.
<?php
//fetches the value of $_get[' user '] and returns ' nobody '
//If it does not exist.
$username = $_get[' user ']?? ' Nobody ';
This is equivalent to:
$username = isset ($_get[' user '])? $_get[' user ': ' Nobody ';
Coalesces can be chained:this'll return
the The ' A//defined value out of $_get[' user ', $_post[' user ', and
//' nobody '.
$username = $_get[' user ']?? $_post[' user ']?? ' Nobody ';
? >
Spacecraft operators (combination comparison) (PHP 7)
The spacecraft operator is used to compare two expressions. When $a is less than, equal to, or greater than $b, it returns 1, 0, or 1, respectively. The principle of comparison is to follow the regular comparison rules of PHP.
<?php
//integers
echo 1 <=> 1;/0
Echo 1 <=> 2;//-1
Echo 2 <=> 1;//1
/Fl Oats
Echo 1.5 <=> 1.5//0
echo 1.5 <=> 2.5;//-1
echo 2.5 <=> 1.5;//1
//Strings
echo "A" <=> "a"; 0
echo "a" <=> "B";//1
echo "B" <=> "a";//1
?>
Defining a constant Array (PHP 7) by define ()
Constants of Array types can now be defined by define (). Only the const definition can be used in PHP5.6.
<?php
define (' ANIMALS ', [
' dog ',
' cat ',
' bird '
]);
Echo Animals[1]; Outputs "Cat"
?>
Anonymous class (PHP 7)
It now supports instantiating an anonymous class with the new class, which can be used to replace the full class definition of "burn after".
<?php
interface Logger {public
function log (string $msg);
Class Application {
private $logger;
Public Function GetLogger (): Logger {return
$this->logger;
}
Public Function Setlogger (Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setlogger (new class implements Logger {public
function log (string $msg) {
echo $msg;
}
});
Var_dump ($app->getlogger ());
>
The above routines will output:
Object (class@anonymous) #2 (0) {
}
Closure::call () (PHP 7)
Closure::call () now has better performance, a short and able temporary binding method to the object closure and call it.
<?php
class A {private $x = 1;}
Pre PHP 7 code
$getXCB = function () {return $this->x;};
$getX = $getXCB->bindto (new A, ' a '); Intermediate Closure
Echo $getX ();
PHP 7+ code
$getX = function () {return $this->x;};
Echo $getX->call (new A);
The above routines will output:
1
1
Provides filtering for Unserialize () (PHP 7)
This feature is designed to provide a more secure way to unpack unreliable data. It prevents potential code injection by means of a whitelist.
<?php
//Converts all objects into __php_incomplete_class object
$data = Unserialize ($foo, ["Allowed_classes "=> false]);
Converts all objects into __php_incomplete_class object except those of MyClass and MyClass2
$data = unserialize ($f OO, ["Allowed_classes" => ["MyClass", "MyClass2"]);
Default behaviour (same as omitting the second argument) that accepts all classes
$data = Unserialize ($foo, ["Allow Ed_classes "=> true]);
Group Use declarations (PHP 7)
Classes, functions, and constants imported from the same namespace can now be imported one at a time through a single use statement.
<?php
//Pre PHP 7 code use
Some\namespace\classa;
Use SOME\NAMESPACE\CLASSB;
Use SOME\NAMESPACE\CLASSC as C;
Use function some\namespace\fn_a;
Use function Some\namespace\fn_b;
Use function Some\namespace\fn_c;
Use const Some\namespace\consta;
Use const SOME\NAMESPACE\CONSTB;
Use const SOME\NAMESPACE\CONSTC;
PHP 7+ Code use
Some\namespace\{classa, CLASSB, classc as C};
Use function some\namespace\{fn_a, Fn_b, fn_c};
Use const Some\namespace\{consta, CONSTB, CONSTC};
? >
To sum up these, the content is from the PHP document, and then useful to the new features that are skipped here will be added ~ ~
The above is the new characteristics of PHP data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!