美文网首页
Modern PHP 阅读笔记之PHP新特性

Modern PHP 阅读笔记之PHP新特性

作者: ben1iu | 来源:发表于2018-10-01 00:51 被阅读0次

    1、命名空间

    1、为什么要使用命名空间:

    1、命名空间 可以解决 多个类名、方法、常量、接口 名字相同的问题。

    大型项目中,命名难免会冲突。命名空间可以解决这个问题

    2、声明命名空间

    <?php
         namespace Oreilly;
         命名空间在PHP文件顶部,<? 标签之后第一行的生命。   命名空间声明语句  以                                                 namespace 开头  随后是一个空格,然后是命名空间的名称  最后;结尾。
     <?php
         namespace Oreilly\ModernPHP
    

    使用 \ 把命名空间和子命名空间分开。

    3、导入和别名

    <?php
        use Symfony\Component\HttpFoundation\Response;
        $response = new Response('Oops',400);
        $response ->send();
    

    别名:

    <?php
        use Symfony\Component\HttpFoundation\Response as Res;
         $response = new Res('Oops',400);
         $response ->send();
    

    导入常量

    <?php
        use constant Namespace\CONT_NAME;
        echo CONT_NAME;
    

    命名空间 使用技巧
    如果想在一个PHP文件中 导入多个类、接口、函数和常量,要在PHP文件的顶部使用多个use语句。

        <?php
        use Symfony\component\HttpFoundation\Request,
               Symfony\component\HttpFoundation\Response,
               Symfony\component\HttpFoundation\Cookie,
    

    建议这样写

        <?php
        use  Symfony\component\HttpFoundation\Request;
        use   Symfony\component\HttpFoundation\Response;
         use  Symfony\component\HttpFoundation\Cookie;
    

    一个文件中使用多个命名空间 (一般 一个文件中定义一个类)

        <?php
        namespace Foo{
        }
        namespace Bar{
        }
    

    全局命名空间

    有些代码 没有命名空间 即可能在全局命名空间中 例如Exception 如果没有使用命名空间之后,可能会导致错误

    使用接口

    class DocumentStore
        {
            protected $data =[];
            public function addDocument(Documentable $document)
            {
                $key = $document->getId();
                $value = $document->getContent();
                $this->data[$key] = $value;
            }
            public function getDocuments()
            {
                return $this->data;
            }
        }
    

    定义

        interface Documentable
        {
            public function getId();
            public function getContent();
        }
    

    HTML 类

        class HtmlDocument implements Documentable
        {
            protected $url;
            public function getId()
            {
                return $this->url;
            }
            public function getContent()
            {
            
            }
        }
    

    使用 DocumentStore类

    <?php
    $documentStore = new DocumentStore();
    $htmlDoc = new HtmlDocument();
    $documentStore->addDocument($htmlDoc);
    

    性状trait

    性状是类的部分实现(即常量,属性和方法) 可以混入一个或多个现有的PHP类中。
    性状有两个作用: 表明类可以做什么,提供模块化实现
    性状能把模块化的实现方式注入多个无关的类中。而且性状还能促进代码重用。
    php不支持多继承,用trait可以实现多继承

    <?php
    trait MyTrait{
        //性状实现
    }
    
    <?php
    class MyClass
    {
        use MyTrait;
        //类的实现
    }
    

    生成器

    1、 创建生成器

    <?php
     function myGenerator(){
        yield 'value1';
        yield 'value2';
        yield 'value3';
    }
    

    2、使用生成器

    <?php
    function makeRange($length){
        $dataset = [];
        for($i =0;$i<$length;$i++){
            $dataset[] = $i;
        }
        return $dataset;
    }
    $customRange = makeRange(1000000);
    foreach($customRange as $i){
        echo $i,PHP_EOL;
    }
    

    正确方法如下

    <?php
    function makeRange($length){
        for ($i=0;$i<$length;$i++){
            yield $i;
        }
    }
    foreach (makeRange(1000000) as $i){
        echo $i,PHP_EOL;
    }
    

    3、闭包

    创建闭包

    <?php
    $closure = function ($name){
        return sprintf('hello %s', $name);
    };
    
    echo $closeure("josh");
    

    相关文章

      网友评论

          本文标题:Modern PHP 阅读笔记之PHP新特性

          本文链接:https://www.haomeiwen.com/subject/prbgoftx.html