zoukankan      html  css  js  c++  java
  • MordenPHP阅读笔记(一)——先跑再说,跑累了再走

    ---恢复内容开始---

      后台一大堆半成品,或者是几乎不成的。。。

    这本书不错,起码是别人推荐的,然后也是比较新的东西,学哪本不是学嘛,关键是得看。

    今儿个网不好,科研所需的代码下不到,看书做笔记吧。

    这本书基本将的是5.4版本后的一些新变化,写的浅显易懂,虽然鄙人走的还不顺溜,跑一跑也摔不到哪儿去,跑累了我有的是走的机会~~

    (一)特性

    一、命名空间

    一个文件一个类,用了命名空间方便互相调用;

     1 //
     2 //Namespace
     3 //
     4 namespace ModernPHPfeaturemingmingkongjian;
     5 function var_dump(){
     6     echo "Shit!"."</br>";
     7 }
     8 
     9 $test="OK";
    10 var_dump($test);
    11 ModernPHPfeaturemingmingkongjianvar_dump();
    12 
    13 //命名空间必须顶头,但一个文件中可以有很多命名空间,然后也可以有子空间
    14 //厂商的命名空间是最顶层的命名空间,用于识别品牌
    15 //旨在解决命名冲突的问题,当然现在应该有比较灵活的其他用法
    16 
    17 //一个比较实用的点:导入和别名
    18 //导入另一个文件夹下的类定义,直接用
    19 require 'index.php';
    20 use aaaa;
    21 $daoru=new aaa;
    22 $daoru->send();
    23 //use是导入,然后在use中设置最懒的别名
    24 //另外,5.6版本后可以实现use 函数
    25 // use func acall;
    26 // acall();

    index.php

     1 <?php
     2 namespace a;
     3 class aaa{
     4     public function send(){
     5         echo "ok";
     6     }
     7 }
     8 
     9 function call(){
    10     echo "func_use is successful.";
    11 }

    二、使用接口

    接口,本来没太懂,看懂了之后简直了,牛逼啊!

    一个接口,大家只要遵守接口规定,就都能用,就这么个意思。

    下面是一个获得内容的接口示例,还可以写更多基于此接口的模块;(其中,模块中getContent的我基本都不会。。。哭)

    <?php
    //
    //Chapter2.P19
    //Feature_Interface
    //
    namespace ModernPHPfeaturejiekou;
    
    
    
    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();
    }
    
    class HtmlDocument implements Documentable{   //声明要用接口;这个是获得url的内容的
        protected $url;
        
        public function __construct($url){
            $this->url=$url;
        }
        
        public function getId(){
            return $this->url;
        }
        
        public function getContent(){
            $ch=curl_init();   //这里的curl是针对url进行操作一个库(相当于)。这个命令是开启一个curl对话,所以下面这些都是一个对话
            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;
        }
    }
    
    
    
    
    $documentStore=new DocumentStore();
    
    $htmlDoc=new HtmlDocument('http://www.baidu.com');
    $documentStore->addDocument($htmlDoc);
    
    
    
    print_r($documentStore->getDocuments());

     另一个模块

     1 class StreamDocument implements Documentable{  //流媒体
     2     protected $resource;
     3     protected $buffer;   //缓冲区大小
     4     
     5     public function __construct($resource,$buffer=4096){
     6         $this->resource=$resource;
     7         $this->buffer=$buffer;
     8     }
     9     
    10     public function getId(){
    11         return 'resource-'.(int)$this->resource;
    12     }
    13     
    14     public function getContent(){
    15         $streamContent='';
    16         rewind($this->resource); //rewind() 函数将文件指针的位置倒回文件的开头
    17         while (feof($this->resource)===false){    //feof() 函数检测是否已到达文件末尾 (eof)。
    18             $streamContent.=fread($this->resource,$this->buffer);
    19         }
    20         
    21         return $streamContent;
    22     }
    23 }

     三、性状

    奇怪的东西。。。

    其实就是为了多重继承或者一对多个不同的类别吧

     1 <?php
     2 //
     3 //Chapter2.P23
     4 //Feature_Trait
     5 //性状
     6 //
     7 
     8 //前面说的接口,是针对同类型的东西,实现相同的功能的;
     9 //这里的性状是针对不同的东西,实现相同的功能
    10 
    11 //基本用法如下
    12 trait traitName{
    13     public function testThis(){
    14         echo "This is how trait works."."<br/>";
    15     }
    16 }
    17 
    18 trait traitMore{
    19     public function testAgain(){
    20         echo "This is multiple use."."<br/>";
    21     }
    22 }
    23 
    24 class className{
    25     use traitName;
    26     use traitMore;
    27     
    28 }
    29 
    30 $classMine=new className();
    31 $classMine->testThis();
    32 $classMine->testAgain();

     四、生成器

    直接上代码

     1 <?php
     2 //
     3 //Chapter2.P26
     4 //Feature_Generator
     5 //生成器
     6 //
     7 
     8 //其实就是在函数中使用了yield语句的东西
     9 //优点在于节省了内存使用情况
    10 //方法是通过动态分配内存进行循环操作
    11 //典型用处是处理csv类数据文件
    12 
    13 namespace ModernPHPfeatureshengchegnqi;
    14 
    15 function getRows($file){
    16     $handle=fopen($file,'rb');
    17     if ($handle===false){
    18         throw new Exception();  //抛出错误原因
    19     }
    20     while (feof($handle)===false) {
    21         yield fgetcsv($handle);
    22     }
    23     fclose($handle);
    24 }
    25 
    26 foreach (getRows('data.csv') as $row){
    27     print_r($row);
    28     echo "<br/>";
    29 }
    30 //当数据文件很大时,效果尤其明显

     五、闭包

    这里闭包基本等于匿名函数

     1 <?php
     2 //
     3 //Chapter2.P29
     4 //Feature_ClosePatch
     5 //闭包或匿名函数
     6 //
     7 
     8 //把函数当作是变量
     9 //然后它就可以像变量一样用来用去了。。
    10 //常用做函数和方法的回调
    11 
    12 namespace ModernPHPfeatureibao;
    13 $var=function ($name){
    14     return sprintf('Hello %s',$name);
    15 };
    16 
    17 echo $var('Andy');
    18 
    19 //做回调
    20 $array=[2,3,4];
    21 $num=array_map(function ($number){  //array_map,将函数作用到数组中的每个值上,每个值都乘以本身,并返回带有新值的数组
    22     return $number+1;
    23 },$array);
    24 print_r($num);

     六、附加状态

    这个没搞懂。。。

    (二)标准

    PHP-FIG的一些约定俗成;

    ---类名称,驼峰式,ShitHappens

    ---方法名称,驼峰式,但首字母小写,shitHappens

    ---缩进统一为4个空格

    ---不写?>结束符号;

    ---{另起一行;

    ---命名空间要有空格;

    ---类中属性和方法必须有可见性声明;

    ---if等控制性结构后面有空格;

     1 <?php
     2 //
     3 //Chapter3.P44
     4 //PHP-FIG puts PSRs
     5 //
     6 
     7 namespace ModernPHPstandard
    ealize;
     8 
     9 use ModernPHPfeatureibao;
    10 use ModernPHPfeaturefujiazhuangtai;
    11 
    12 class ShitHappens
    13 {
    14     public $a;
    15     
    16     public function suck()
    17     {
    18         if ($this->a===false){
    19             return true;
    20         }
    21     }
    22 }

    ----------------------

    后面的都是讲述的东西,有需要的我再写吧。 

  • 相关阅读:
    SpringBoot项目设置maven打包时间
    SpringBoot热部署配置
    Git笔记
    SpringBoot LogBack日志配置
    CURL使用教程
    Linux 安装Docker及使用
    转发和重定向的区别
    16周作业
    16
    15周
  • 原文地址:https://www.cnblogs.com/andy1202go/p/5409312.html
Copyright © 2011-2022 走看看