首页 > 后端开发 > php教程 > PHP7新增 功能,php7新增功能_PHP教程

PHP7新增 功能,php7新增功能_PHP教程

WBOY
发布: 2016-07-12 09:03:33
原创
1222 人浏览过

PHP7新增 功能,php7新增功能

php7发布已有半月,最近有时间了解一下php7的新特性,当然,这个版本最大的特点是性能的提升。在下并非高手,欢迎大家指出错误,同时期待共同交流

1.在use语句增加了group支持
use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };


2.增加??操作符
isset($_GET['mykey']) ? $_GET['mykey'] : ""笨重
$_GET['mykey'] ?: "" 当mykey不存在时会报一个E_NOTICE
$_GET['mykey'] ?? 'defaultvalue' 安全不会报E_NOTICE
$username = $_GET['user'] ?? 'nobody';

 

3. 64位PHP7字符串长度可以超过2^31次方字节。

 

4.增加Closure::call支持
Closure::call将一个闭包函数动态绑定到一个新的对象实例并调用执行该函数,

<span> 1</span> <?<span>php
</span><span> 2</span> <span>class</span><span> Value {
</span><span> 3</span> <span>protected</span> <span>$value</span><span>;
</span><span> 4</span> 
<span> 5</span> <span>public</span> <span>function</span> __construct(<span>$value</span><span>) {
</span><span> 6</span> <span>$this</span>->value = <span>$value</span><span>;
</span><span> 7</span> <span>}
</span><span> 8</span> 
<span> 9</span> <span>public</span> <span>function</span><span> getValue() {
</span><span>10</span> <span>return</span> <span>$this</span>-><span>value;
</span><span>11</span> <span>}
</span><span>12</span> <span>}
</span><span>13</span> 
<span>14</span> <span>$three</span> = <span>new</span> Value(3<span>);
</span><span>15</span> <span>$four</span> = <span>new</span> Value(4<span>);
</span><span>16</span> 
<span>17</span> <span>$closure</span> = <span>function</span> (<span>$delta</span>) { <span>var_dump</span>(<span>$this</span>->getValue() + <span>$delta</span><span>); };
</span><span>18</span> <span>$closure</span>->call(<span>$three</span>, 4<span>);
</span><span>19</span> <span>$closure</span>->call(<span>$four</span>, 4<span>);
</span><span>20</span> ?>
<span>21</span> <span>//</span><span> outputs int(7),int(8)</span>
登录后复制

5.双引号字符串和heredocs里面支持使用\u{xxxxx}来声明unicode字符。

6.define对数组的支持

<span>1</span> <span>define</span>('ANIMALS', <span>array</span><span>(
</span><span>2</span> 'dog',
<span>3</span> 'cat',
<span>4</span> 'bird'
<span>5</span> <span>));
</span><span>6</span> <span>echo</span> ANIMALS[1]; <span>//</span><span> outputs "cat"</span>
登录后复制

7.增加比较运算符<=>
$a <=> $b
如果a等于b则为0
如果a大于b则为1
如果a小于b则为-1

8.php全局保留字可以声明使用

<span>1</span> <span>class</span><span> View {
</span><span>2</span>     <span>public</span> <span>function</span> <span>include</span>(View <span>$view</span><span>) {
</span><span>3</span>         <span>//</span><span>...</span>
<span>4</span> <span>    }
</span><span>5</span> }
登录后复制

include关键字可以当普通字符串关键字一样被使用

9.标量类型(int,float,string,bool)支持
增加declare(strict_types=1)指令声明是否严格类型校验,
当在文件头声明declare(strict_types=1)

<span>1</span> <<span>php
</span><span>2</span> 
<span>3</span> <span>declare</span>(strict_types=1<span>);
</span><span>4</span> <span>function</span> add(<span>float</span> <span>$a</span>, <span>float</span> <span>$b</span>): <span>float</span><span> {
</span><span>5</span>     <span>return</span> <span>$a</span> + <span>$b</span><span>;
</span><span>6</span> <span>}
</span><span>7</span> add(1, 2); <span>//</span><span> float(3)</span>
登录后复制

以上代码如果不开启declare(strict_types=1)或许declare(strict_types=0),php将自动转换参数和返回值到指定类型,
开启declare(strict_types=1),如果参数类型不是flaot或许返回类型不是float则抛出错误

 

10.增加接口为用户层提供安全方便的随机数生成器。RFC: https://wiki.php.net/rfc/easy_userland_csprng (后续再议)

11.增加了yield from 操作符。https://wiki.php.net/rfc/generator-delegation (后续再议)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1080765.htmlTechArticlePHP7新增 功能,php7新增功能 php7发布已有半月,最近有时间了解一下php7的新特性,当然,这个版本最大的特点是性能的提升。在下并非高手...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板