Anonymous Class (PHP 7)
It is now supported to instantiate an anonymous class with the new class, which can be used instead of some full-burn class definitions.
<?phpinterface 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, skilful way to temporarily bind a method to a closure on an object and invoke it.
<?phpclass A {Private $x = 1;} Pre PHP 7 CODE$GETXCB = function () {return $this->x;}; $getX = $getXCB->bindto (new A, ' a '); Intermediate Closureecho $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 of unpacking unreliable data. It prevents potential code injection by using a whitelist approach.
<?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 ($foo, [ "Allowed_classes" = ["MyClass", "MyClass2"]);//default behaviour (same as omitting the second argument) that accepts All Classes$data = Unserialize ($foo, ["allowed_classes" = true]);
Group Use declarations (PHP 7)
Classes, functions, and constants imported from the same namespace can now be imported once with a single use statement.
<?php//Pre php 7 codeuse 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+ codeuse Some\namespace\{classa, ClassB , ClassC as C};use function some\namespace\{fn_a, Fn_b, Fn_c};use const Some\namespace\{consta, CONSTB, CONSTC};? >