PHP 5.4 相关知识
新特征与改动
此次更新的要害新特征,包罗:新增traits,更精简的Array数组语法,供测试运用的内建webserver,可以闭包运用的$this指针,实例化类成员拜访,
PHP 5.4.0 功能大幅提拔, 修复超越100个bug. 废弃了register_globals, magic_quotes以及平安形式。 别的值得一提的是多字节支撑曾经默许启用了,default_charset从ISO-8859-1曾经变为UTF-8. 默许发送“Content-Type: text/html; charset=utf-8”,你再也不需求在HTML里写meta tag,也无需为UTF-8兼容而传送额定的header了。
Traits
Traits (横向重用/多重承继)是一组构造很像“类”(但不克不及实例化)的办法,它可以闪开发人员在分歧的类中轻松地重用办法。 PHP为单承继言语,子类只能承继一个父类,于是Traits来了。
Traits的最佳使用是多类之间可以共享一样的函数。打个比如,我们要做个网站,需求运用Facebook和Twitter的APIs。我们要建 2个类,假如是以前,我们需求写一个cURL的办法而且复制/粘贴到两个类中。目前不必了,运用Traits重用代码吧,此次真正地遵照了 DRY(Don’t Repeat Yourself)准则。
<span style="COLOR: rgb(0,100,0)">/** cURL wrapper trait */
<span style="COLOR: rgb(0,100,0)"> trait cURL
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> public function curl($url)
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> $ch = curl_init();
<span style="COLOR: rgb(0,100,0)"> curl_setopt($ch, CURLOPT_URL, $url);
<span style="COLOR: rgb(0,100,0)"> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
<span style="COLOR: rgb(0,100,0)"> $output = curl_exec($ch);
<span style="COLOR: rgb(0,100,0)"> curl_close($ch);
<span style="COLOR: rgb(0,100,0)"> return $output;
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> /** Twitter API Class */
<span style="COLOR: rgb(0,100,0)"> class Twitter_API
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> use cURL; // use trait here
<span style="COLOR: rgb(0,100,0)"> public function get($url)
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return json_decode($this->curl('http://api.twitter.com/'.$url));
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> /** Facebook API Class */
<span style="COLOR: rgb(0,100,0)"> class Facebook_API
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> use cURL; // and here
<span style="COLOR: rgb(0,100,0)"> public function get($url)
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return json_decode($this->curl('http://graph.facebook.com/'.$url));
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> $facebook = new Facebook_API();
<span style="COLOR: rgb(0,100,0)"> echo $facebook->get('500058753')->name; // Rasmus Lerdorf
<span style="COLOR: rgb(0,100,0)"> /** Now demonstrating the awesomeness of PHP 5.4 syntax */
<span style="COLOR: rgb(0,100,0)"> echo (new Facebook_API)->get('500058753')->name;
<span style="COLOR: rgb(0,100,0)"> $foo = 'get';
<span style="COLOR: rgb(0,100,0)"> echo (new Facebook_API)->$foo('500058753')->name;
<span style="COLOR: rgb(0,100,0)"> echo (new Twitter_API)->get('1/users/show.json?screen_name=rasmus')->name;
看清楚了吗?没有?那你来瞅瞅更简略的例子
<span style="COLOR: rgb(0,100,0)">trait Hello
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> public function hello()
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return 'Hello';
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> trait Cichui
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> public function cichui()
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return ' cichui';
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> class HelloCichui
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> use Hello, Cichui;
<span style="COLOR: rgb(0,100,0)"> public function the_end()
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return '!';
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> $o = new HelloCichui;
<span style="COLOR: rgb(0,100,0)"> echo $o->hello(), $o->cichui(), $o->the_end();
<span style="COLOR: rgb(0,100,0)"> echo (new Hello)->hello(), (new Cichui)->cichui(), (new HelloCichui)->the_end();
内建的Web-Sever
在Web开拓中,Apache HTTPD是PHP的最佳拍档。有时,你开拓时用不上需求装备httpd.conf的apache大杀器,而只需求一个可以在敕令行中运用的超小型 Webserver. 感激PHP(先感激国度),PHP 5.4此次内建了CLI Web server。(PHP CLI webserver仅供开拓运用,回绝产物用处)
举个栗子(windows平台):
步调一:树立web根目次, Router和Index
在硬盘根目次(比方C盘)树立一个public_html目次,目次里新建一个router.php文件,把以下代码复制粘贴进去:
<span style="COLOR: rgb(0,100,0)"> // router.php
<span style="COLOR: rgb(0,100,0)"> if (preg_match('#\.php$#', $_SERVER['REQUEST_URI']))
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> require basename($_SERVER['REQUEST_URI']); // serve php file
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> else if (strpos($_SERVER['REQUEST_URI'], '.') !== false)
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return false; // serve file as-is
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> ?>
再来新建一个index.php文件,复制粘贴以下代码:
<span style="COLOR: rgb(0,100,0)"> // index.php
<span style="COLOR: rgb(0,100,0)"> echo 'Hello cichui.com Readers!';
<span style="COLOR: rgb(0,100,0)"> ?>
编纂你的php.ini文件,找到”include_path”一行,把c:\public_html添加进去(分号分隔):
1include_path = ".;C:\php\PEAR;C:\public_html"
存盘退出,看下一步
步调二:运转Web-Server
切换到php的装置目次,敲下最要害的敕令—运转Web-server
php -S 0.0.0.0:8080 -t C:\public_html router.php
开端了吗?不要封闭窗口,假如历程封闭Web server也跟着封闭了。
翻开阅读器:拜访http://localhost:8080/index.php吧,
Hello cichui.com Readers!
看到了吧?对,就是这个!
提醒1:你可以思索自建一个php-server.bat的批处置,扔到桌面上今后就可以双击启动了。
提醒2:运用0.0.0.0而不是localhost,可以包管外网不会拜访到你的web serve。
精简的Array数组语法
PHP 5.4为您送上精简的array数组语法:
<span style="COLOR: rgb(0,100,0)"> $fruits = array('apples', 'oranges', 'bananas'); // "old" way
<span style="COLOR: rgb(0,100,0)"> // 学Javascript的数组了
<span style="COLOR: rgb(0,100,0)"> $fruits = ['apples', 'oranges', 'bananas'];
<span style="COLOR: rgb(0,100,0)"> // 联系关系数组
<span style="COLOR: rgb(0,100,0)"> $array = [
<span style="COLOR: rgb(0,100,0)"> 'foo' => 'bar',
<span style="COLOR: rgb(0,100,0)"> 'bar' => 'foo'
<span style="COLOR: rgb(0,100,0)"> ];
当然,旧语法照旧有用,我们多了一种选择。
数构成员拜访解析(Array dereferencing*)
处置数组再也不需求暂时变量了。
假定我们需求获取Fang Bin Xin的middle name,
<span style="COLOR: rgb(0,100,0)"> echo explode(‘ ‘, ‘Fang Bin Xin’)[1]; // Bin
PHP 5.4之前,我们需求如许:
<span style="COLOR: rgb(0,100,0)"> $tmp = explode(‘ ‘, ‘Fang Bin Xin’);
<span style="COLOR: rgb(0,100,0)"> echo $tmp[1]; // Bin
目前,我们可以如许玩了:
<span style="COLOR: rgb(0,100,0)"> echo end(explode(‘ ‘, ‘Fang Bin Xin’)); // Xin
再来个高级点的例子:
<span style="COLOR: rgb(0,100,0)"> function foobar()
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return ['foo' => ['bar' => 'Hello']];
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> echo foobar()['foo']['bar']; // Hello
*瓷锤注: Array dereferencing直译应为数组解除援用,结果欠安。其实更精确的翻译应为:“对函数返回后果的数构成员拜访解析支撑”,详见PHP官方分析。
匿名函数中的$this
目前,你可以在类实例中经过$this援用一个匿名函数(也叫闭包函数)
<span style="COLOR: rgb(0,100,0)">class Foo
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> function hello() {
<span style="COLOR: rgb(0,100,0)"> echo 'Hello Cichui!';
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> function anonymous()
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return function() {
<span style="COLOR: rgb(0,100,0)"> $this->hello(); // 之前是不成能这么玩的
<span style="COLOR: rgb(0,100,0)"> };
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> class Bar
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> function __construct(Foo $o) // object of class Foo typehint
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> $x = $o->anonymous(); // get Foo::hello()
<span style="COLOR: rgb(0,100,0)"> $x(); // execute Foo::hello()
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> new Bar(new Foo); // Hello Cichui!
其实以前也能迁就用,就是有点费力:
<span style="COLOR: rgb(0,100,0)">function anonymous()
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> $that = $this; // $that is now $this
<span style="COLOR: rgb(0,100,0)"> return function() use ($that) {
<span style="COLOR: rgb(0,100,0)"> $that->hello();
<span style="COLOR: rgb(0,100,0)"> };
<span style="COLOR: rgb(0,100,0)"> }
无论php.ini中若何装备,short_open_tag, 也就是 交换以前的了。
支撑二进制直接量
八进制(oct),前面加0;十六进制(hex),前面加0x;二进制(bin),目前在前面加0b就可以了
<span style="COLOR: rgb(0,100,0)"> echo 0b11111; // PHP 5.4支撑二进制了
<span style="COLOR: rgb(0,100,0)"> echo 31; // 十进制
<span style="COLOR: rgb(0,100,0)"> echo 0x1f; // 十六进制
<span style="COLOR: rgb(0,100,0)"> echo 037; // 八进制
函数类型提醒
自PHP 5.1起,类型提醒支撑对象和数组,PHP 5.4开端支撑callable。
<span style="COLOR: rgb(0,100,0)"> function my_function(callable $x)
<span style="COLOR: rgb(0,100,0)"> {
<span style="COLOR: rgb(0,100,0)"> return $x();
<span style="COLOR: rgb(0,100,0)"> }
<span style="COLOR: rgb(0,100,0)"> function my_callback_function(){return 'Hello Cichui!';}
<span style="COLOR: rgb(0,100,0)"> class Hello{static function hi(){return 'Hello Cichui!';}}
<span style="COLOR: rgb(0,100,0)"> class Hi{function hello(){return 'Hello Cichui!';}}
<span style="COLOR: rgb(0,100,0)"> echo my_function(function(){return 'Hello Cichui!';}); // 闭包函数
<span style="COLOR: rgb(0,100,0)"> echo my_function('my_callback_function'); // 回调函数
<span style="COLOR: rgb(0,100,0)"> echo my_function(['Hello', 'hi']); // 类名,静态办法
<span style="COLOR: rgb(0,100,0)"> echo my_function([(new Hi), 'hello']); // 类名,办法名
高精度计时器
此次引入了$_SERVER['REQUEST_TIME_FLOAT']数组变量,微秒级精度(百万分之一秒,float类型)。关于计算剧本运转工夫会十分有效:
<span style="COLOR: rgb(0,100,0)"> 1echo 'Executed in ', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)
- 1义乌华睿软件有关进销存管理软件的知识
- 2企业微信公共账号怎么认证?
- 3B2B行业网站页面内容怎样设计
- 4网站改版切勿盲目跟风
- 5必须在掸掉家具外表尘埃后轻轻地擦洗
- 6PHP缓存技术
- 7最重要是主材选购占办公室装修中费用一半以上
- 8网站帮助中心和联系我们等内容是否屏蔽蜘蛛
- 9gov.cn域名注册说明
- 10域名常识
- 11ERP系统与SCM系统七大重叠功能
- 12连锁在线会员管理软件,纳客连锁会员管理软件
- 13百度分享是否有利于排名
- 14准确选择IDC机房主机托管服务
- 15企业做SEO有什么意义?
- 16参考文献不仅具有学术价值也具有多项其他功能
- 17网站收录及关键词排名分析
- 18把冰箱内的食物拿出来,替冰箱大搞一次卫生
- 19义乌华睿软件企业管理软件新认识之一
- 20SEOer要学会寻找优质外链
- 21网站建设公司分析“九大”采纳阅历总计
- 22南昌网站优化提醒SEO优化的是什么
- 23合肥最好的网络公司告诉大家快速提高网站权重
- 24怎样把网站建设需求分析做到最好
- 25用垃圾站点流量也能做出正规网站
- 26母乳喂养宝宝应斜躺在妈妈怀里
- 27利用路由器加速自己的宽带
- 28管理软件企业管理六大痛点
- 29简述影响网站的四个主要因素
- 30企业优化网站的方案