PHP7 anonymous classes, Introduction to new features such as import class and closure usage

Source: Internet
Author: User
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};? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.