您好,欢迎访问一九零五行业门户网

MordenPHP阅读笔记(一)先跑再说,跑累了再走 - 韧还

---恢复内容开始---
后台一大堆半成品,或者是几乎不成的。。。
这本书不错,起码是别人推荐的,然后也是比较新的东西,学哪本不是学嘛,关键是得看。
今儿个网不好,科研所需的代码下不到,看书做笔记吧。
这本书基本将的是5.4版本后的一些新变化,写的浅显易懂,虽然鄙人走的还不顺溜,跑一跑也摔不到哪儿去,跑累了我有的是走的机会~~
(一)特性
一、命名空间
一个文件一个类,用了命名空间方便互相调用;
1 // 2 //namespace 3 // 4 namespace modernphp\feature\mingmingkongjian; 5 function var_dump(){ 6 echo shit!.; 7 } 8 9 $test=ok;10 var_dump($test);11 \modernphp\feature\mingmingkongjian\var_dump();12 13 //命名空间必须顶头,但一个文件中可以有很多命名空间,然后也可以有子空间14 //厂商的命名空间是最顶层的命名空间,用于识别品牌15 //旨在解决命名冲突的问题,当然现在应该有比较灵活的其他用法16 17 //一个比较实用的点:导入和别名18 //导入另一个文件夹下的类定义,直接用19 require 'index.php';20 use a\aaa;21 $daoru=new aaa;22 $daoru->send();23 //use是导入,然后在use中设置最懒的别名24 //另外,5.6版本后可以实现use 函数25 // use func a\call;26 // \a\call();
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 modernphp\feature\jiekou;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..
;15 }16 }17 18 trait traitmore{19 public function testagain(){20 echo this is multiple use..
;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 modernphp\feature\shengchegnqi;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
;29 }30 //当数据文件很大时,效果尤其明显
五、闭包
这里闭包基本等于匿名函数
1 php 2 // 3 //chapter2.p29 4 //feature_closepatch 5 //闭包或匿名函数 6 // 7 8 //把函数当作是变量 9 //然后它就可以像变量一样用来用去了。。10 //常用做函数和方法的回调11 12 namespace modernphp\feature\bibao;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 modernphp\standard\realize; 8 9 use modernphp\feature\bibao;10 use modernphp\feature\fujiazhuangtai;11 12 class shithappens13 {14 public $a;15 16 public function suck()17 {18 if ($this->a===false){19 return true;20 }21 }22 }
----------------------
后面的都是讲述的东西,有需要的我再写吧。
其它类似信息

推荐信息