zoukankan      html  css  js  c++  java
  • php新特性--持续更新

    命名空间

      在其他语言中不算新鲜事,但php是5.3.0中引入,具体定义就不复述了,其主要作用是 封装和组织相关php类 。命名空间被引入之前php主要是通过Zend方式组织代码,这种方式带来的问题是类名依赖于目录(虽然命名空间之后规范要求也要和目录一样)导致类名特别特别长,如:Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query。

      命名空间把代码放到一个逻辑上的空间中,可以和其他开发者编写的代码一起使用,是现代php生态系统的基础。详细参考php手册。

      声明命名空间:

    <?php
        namespace Myspace; // 使用namespace关键字,而且必须是第一行

      导入命名空间:

    <?php
        /* 关键字 use ;
         * 位置 <?php  和 namespace 之后
         * 不必写  php会假定导入的是完全限定的命名
         * 必须出现在全局作用域中,因为这个关键字在编译的时候处理
        use SymfonyComponentHttpFoundationResponse as Res;
        
        $respon = new Res('Oops', 400);
        $respon->send();
    5.6之后支持导入常量和函数,语法如下:
    
    <?php
        use func NamespacefunctionName
        functionName();
    
    <?php
        use constant NamespaceCONST_NAME;
        echo CONST_NAME;

    接口

      接口不是新特性但是很重要,接口是两个php对象的契约。其目的不是让一个对象依赖另一个对象的身份,而是依赖另一个对象的能力。接口把我们的代码和依赖解耦,而且允许我们的代码依赖任何实现了预期接口的第三方代码。我们不关心第三方代码如何实现接口,只去关心他有没有去实现接口。

         如果我们写的类去处理特定的对象, 那么类的功能就被限定了,只能处理那个类。但是我们的对象如果是处理的接口,那么代码立即就能知道如何处理实现这一接口的任何对象,我们的代码不管接口如何实现只需要关心有没有实现。
     
    文档处理类实现
    <?php
    class DocumentStore {
        protected $data = [];
        
        /**
         * 参数限定为 Documentable 对象,这是一个接口
         */
        public function addDocument(Documentable $document) {
            $key = $document->getId();
            $value = $document->getContent();
            $this->data[$key] = $value;
        }
    
        public function getDocuments() {
            return $this->data;
        }
    }

    这个是我们的文档处理类,它面向的是接口操作  Documentable 实现:

    <?php
    interface Documentable {
        public function getId();
        public function getContent();
    }

    具体实现接口的类,比如是从html获得的文档 实现:

    <?php
    class HtmlDocument implements Documentable {
        protected $url;
        public function __construct($url)
        {
            $this->url = $url;
        }
        public function getId()
        {
            return $this->url;
        }
        public function getContent()
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
            $html = curl_exec($ch);
            curl_close($ch);
            return $html;
        }
    }

     读取流数据的文档 实现:

    <?php
    class StreamDocument implements Documentable {
        protected $resource;
        protected $buffer;
        public function __construct($resource, $buffer = 4096)
        {
            $this->resource = $resource;
            $this->buffer = $buffer;
        }
        public function getId()
        {
            return 'resource-' . (int)$this->resource;
        }
        public function getContent()
        {
            $streamContent = '';
            rewind($this->resource);
            while (feof($this->resource) === false) {
                $streamContent .= fread($this->resource, $this->buffer);
            }
            return $streamContent;
        }
    }
    具体使用 :
    <?php
    require 'Documentable.php';
    require 'DocumentStore.php';
    require 'HtmlDocument.php';
    require 'StreamDocument.php';
    require 'CommandOutputDocument.php';
     
    $documentStore = new DocumentStore();
     
    // Add HTML document
    $htmlDoc = new HtmlDocument('http://php.net');
    $documentStore->addDocument($htmlDoc);
     
    // Add stream document
    $streamDoc = new StreamDocument(fopen('stream.txt', 'rb'));
    $documentStore->addDocument($streamDoc);
     
    // Add terminal command document
    $cmdDoc = new CommandOutputDocument('cat /etc/hosts');
    $documentStore->addDocument($cmdDoc);
     
    print_r($documentStore->getDocuments());

    需要说明的是参数类型 addDocument 参数类型限定为 Documentable 实现该接口的对象都可以做参数。 

    性状( trait )

        性状是类的部分实现,可以混入一个或者多个现有的类实现中,有两个作用:

      1表明类可以做什么;

      2 提供模块化实现;

     使用场景:

      我们做面向对象的开发的时候都会通过基类实现基本功能,完后子类具体实现详细的功能,各类之间有明显的自然的继承关系,如果有一个逻辑既不属于A类也不属于B类,那么在性状出现之前我们怎么解决:

      解决办法一:做一个父类 让A, B都继承,这样做的缺点是,强制把两个不相关的类继承同一父类,结构混乱破坏了封装。

      解决方法二:做一个接口,让A, B都去实现这个接口,强于上一个方法,但是缺点是相同的逻辑会在多个类中实现,代码逻辑冗余,加大维护成本。

      解决办法三:使用性状(trait)推荐做法。

    定义性状:

    <?php
        // 推荐把性状当类看待,一个文件定义一个性状
        trait MyTrait {
            protected $p1;
            public $p2;
    
            public function f1() {
    
            }
        }

    使用性状:

    <?php
    class MyClass {
        use MyTrait;
    }

    之后实例对象就可以使用性状里的属性方法就像使用本类的一样;php解释器会把性状的代码复制到类定义中,有点像c语言中的宏。

    生成器(generator)

      5.5引入的新特性,可理解为迭代器,但和普通的迭代器不同不需要实现一堆Iterator接口。生成器每次迭代都是实时计算出值,所以节省很大内存。生成器无法快进和后退,而且不能多次迭代同意生成器,如果需要可以重建和克隆生成器。

    <?php
        function myGenerator () {
            yield 'value1';
            yield 'value1';
            yield 'value1';
        }
    <?php
    foreach (myGenerator() as $yieldValue) {
        echo $yieldValue, PHP_EOL;
    }

    处理大文件:

    <?php
    function getRows($file) {
        $handle = fopen($file, 'rb');
        if (!$handle) {
            throw new Exception();
        }
        while (!feof($handle)) {
            yield fgetcsv($handle);
        }
        fclose($handle);
    }
    foreach (getRows('data.csv') as $row) {
        print_r($row);
    }

    闭包

      指在创建时封装周围环境的函数,即使闭包所在的环境不存在了,闭包中封装的状态也还存在。

    <?php
    $closure = function ($name) {
        return sprintf('Hello %s', $name);
    };
    echo $closure("Josh");
    // Outputs --> "Hello Josh"

      php中闭包不会主动封装状态,需要使用 use 或 bindTo 把状态附件到闭包上。

    <?php
    function enclosePerson($name) {
        return function ($doCommand) use ($name) {
            return sprintf('%s, %s', $name, $doCommand);
        };
    }
    // Enclose "Clay" string in closure
    $clay = enclosePerson('Clay');
    // Invoke closure with command
    echo $clay('get me sweet tea!');
    // Outputs --> "Clay, get me sweet tea!"
    <?php
    class App
    {
        protected $routes = [];
        protected $responseStatus = '200 OK';
        protected $responseContentType = 'text/html';
        protected $responseBody = 'Hello world';
        public function addRoute($routePath, $routeCallback)
        {
            $this->routes[$routePath] = $routeCallback->bindTo($this, __CLASS__);
        }
        public function dispatch($currentPath)
        {
            foreach ($this->routes as $routePath => $callback) {
                if ($routePath === $currentPath) {
                    $callback();
                }
            }
            header('HTTP/1.1 ' . $this->responseStatus);
            header('Content-type: ' . $this->responseContentType);
            header('Content-length: ' . mb_strlen($this->responseBody));
            echo $this->responseBody;
        }
    }
    $app = new App();
    $app->addRoute('/users/josh', function () {
        $this->responseContentType = 'application/json;charset=utf8';
        $this->responseBody = '{"name": "Josh"}';
    });
    $app->dispatch('/users/josh');

    Zend OPcache

         php解释器会把代码先编译成字节码,如果可以把编译好的字节码缓存起来,源代码不变的话可以直接读取字节码提升执行效率。目前有很多字节码缓存扩展。启用的话先需要编译扩展

    加载配置。如果启用过xdebug需要先加载opcache 再加载xdebug。

    内置http服务器

      一般做测试用,不用麻烦去配置环境,不可以用于线上环境。 php  -S localhost:4000  启动起来的话访问信息都在终端打印,ctrl + c 结束,详细参考php手册。

  • 相关阅读:
    P1828 [USACO3.2]香甜的黄油 Sweet Butter 题解
    P2058 海港 题解
    浅谈三分算法
    海伦公式的证明
    一年一回首
    再谈单调队列优化 & 背包九讲
    浅谈单调队列
    P1440 求m区间内的最小值 题解
    CF1374B Multiply by 2, divide by 6 题解
    组合数、杨辉三角与递推算法
  • 原文地址:https://www.cnblogs.com/anyeshe/p/5746077.html
Copyright © 2011-2022 走看看