Heim >Backend-Entwicklung >PHP-Tutorial >PHP7新增 功能,php7新增功能_PHP教程

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

WBOY
WBOYOriginal
2016-07-12 09:03:331182Durchsuche

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的新特性,当然,这个版本最大的特点是性能的提升。在下并非高手...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn