Home>Article>Backend Development> Integrate features from PHP5.X to PHP7.1.x

Integrate features from PHP5.X to PHP7.1.x

coldplay.xixi
coldplay.xixi forward
2021-01-20 09:47:51 1601browse

Integrate features from PHP5.X to PHP7.1.x

Recommended (free):PHP7

So many good features and good methods, why not use them , I also hope that PHP will get better and better.
Here we organize all the new features of PHP 5.1, PHP5.2, PHP5.3, PHP5.4, PHP5.5, PHP5.6, PHP7, PHP7.1, ready for everyone to learn and use

Buid-in web server has a simple built-in web server

To use the current directory as the Root Document, you only need this command:

php -S localhost:3300

Also Specify other paths

php -S localhost:3300 -t /path/to/root

You can also specify routes

php -S localhost:3300 router.php

Namespace (php5.3)

The delimiter of the namespace is a backslash\

namespace fox\lanmps\Table; class Select {}

Get the complete category name

The functions of namespace alias classes and namespace short versions were introduced in PHP5.3. Although this does not work for string class names

use Some\Deeply\Nested\Namespace\FooBar; // does not work, because this will try to use the global `FooBar` class $reflection = new ReflectionClass('FooBar'); echo FooBar::class;

To solve this problem adopt the new FooBar::class syntax, which returns the full class name of the class

Namespace use operation Symbol starts to support the import of functions and constants

namespace Name\Space { const FOO = 42; function f() { echo __FUNCTION__."\n"; } } namespace { use const Name\Space\FOO; use function Name\Space\f; echo FOO."\n"; f(); }

Output
42
Name\Space\f

Group use declarations

Classes, functions and constants imported from the same namespace can now be imported at once with a single use statement.

//PHP7之前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;// PHP7之后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};

Support delayed static binding

static keyword to reference the current class, which implements delayed static binding

class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // 这里实现了延迟的静态绑定 } } class B extends A { public static function who() { echo __CLASS__; } } B::test();

Output result:
B

Support goto statement

Most computer programming languages support the unconditional goto statement goto. When the program executes the goto statement, it will turn to the goto statement. Execution continues at the program location indicated by the label. Although the goto statement may lead to unclear program flow and reduced readability, it has its own unique convenience in certain situations, such as interrupting deeply nested loops and if statements.

goto a; echo 'Foo'; a: echo 'Bar'; for($i=0,$j=50; $i<100; $i++) { while($j--) { if($j==17) goto end; } } echo "i = $i"; end: echo 'j hit 17';

Support closures, Lambda/Anonymous functions

The concepts of closure (Closure) functions and Lambda functions come from the field of functional programming. JavaScript, for example, is one of the most common languages that supports closures and lambda functions.
In PHP, we can also create functions while the code is running through create_function(). But there is a problem: the created function is only compiled at runtime and is not compiled into executable code at the same time as other codes. Therefore, we cannot use executable code caches like APC to improve code execution efficiency.
In PHP5.3, we can use Lambda/anonymous functions to define some temporary (use and throw away) functions as callback functions for array_map()/array_walk() and other functions.

echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world'); // 输出 helloWorld $greet = function($name) { printf("Hello %s\r\n", $name); }; $greet('World'); $greet('PHP'); //...在某个类中 $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); };

Magic methods __callStatic() and __invoke()

There is originally a magic method __call() in PHP. When the code calls an object that is not This magic method will be automatically called when the method exists. The new __callStatic() method is only used for static class methods. When trying to call a static method that does not exist in the class, the __callStatic() magic method will be automatically called.

class MethodTest { public function __call($name, $arguments) { // 参数 $name 大小写敏感 echo "调用对象方法 '$name' " . implode(' -- ', $arguments). "\n"; } /** PHP 5.3.0 以上版本中本类方法有效 */ public static function __callStatic($name, $arguments) { // 参数 $name 大小写敏感 echo "调用静态方法 '$name' " . implode(' -- ', $arguments). "\n"; } } $obj = new MethodTest; $obj->runTest('通过对象调用'); MethodTest::runTest('静态调用'); // As of PHP 5.3.0

The output after the above code is executed is as follows:
Call the object method 'runTest' –- Call the static method 'runTest' through the object call –- Static call
When calling the object in the form of a function, __invoke () method will be called automatically.

class MethodTest { public function __call($name, $arguments) { // 参数 $name 大小写敏感 echo "Calling object method '$name' " . implode(', ', $arguments). "\n"; } /** PHP 5.3.0 以上版本中本类方法有效 */ public static function __callStatic($name, $arguments) { // 参数 $name 大小写敏感 echo "Calling static method '$name' " . implode(', ', $arguments). "\n"; } } $obj = new MethodTest; $obj->runTest('in object context'); MethodTest::runTest('in static context'); // As of PHP 5.3.0

Nowdoc syntax

Usage is similar to Heredoc, but uses single quotes. Heredoc needs to be declared using double quotes.
Nowdoc does not do any variable parsing, which is very suitable for passing a piece of PHP code.

// Nowdoc 单引号 PHP 5.3之后支持 $name = 'MyName'; echo <<<'EOT' My name is "$name". EOT; //上面代码输出 My name is "$name". ((其中变量不被解析) // Heredoc不加引号 echo <<
      

Supports initialization of static variables, class members and class constants through Heredoc.

// 静态变量 function foo() { static $bar = <<
Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete