php7 新内容

1、use增强

以thinkphp5.0为例

namespace app\home\controller;
use think\{Loader,Controller,Captcha,Request};

2、匿名类

<?php
class Outer
{
    private $prop = 1;
    protected $prop2 = 2;
    protected function func1()
    {
        return 3;
    }
    public function func4()
    {
        return $this->prop;
    }
    public function func2()
    {
        return new class() extends Outer {
          
            public function func3()
            {
            return $this->prop2 + $this->func4() + $this->func1();
            }
        };
    }
}

echo (new class() extends Outer{})->func4();
echo (new Outer)->func2()->func3();
<?php
$arr = array();
for ($i=0; $i<3; $i++){
  $arr[] = new class($i){
    public $index=0;
    function __construct($i)
    {
      $this->index = $i;
      echo 'create</br>';
    }
    public function getVal(){
      echo $this->index;
    }
  };
}
$arr[1]->getVal();
echo '<br>';
var_dump($arr[1]);

Generator 加强

<?php
$input = <<<'EOF'
1;PHP;Likes dollar signs
2;Python;Likes whitespace
3;Ruby;Likes blocks
EOF;

function input_parser($input) {
    foreach (explode("\n", $input) as $line) {
        $fields = explode(';', $line);
        $id = array_shift($fields);

        yield $id => $fields;
    }
}

foreach (input_parser($input) as $id => $fields) {
    echo "$id:<br>";
    echo "    $fields[0]<br>";
    echo "    $fields[1]<br>";
}
<?php
function gen_three_nulls() {
    foreach (range(1, 3) as $i) {
        yield;
    }
}

var_dump(iterator_to_array(gen_three_nulls()));
<?php
function gen()
{
    yield 1;
    yield 2;

    yield from gen2();
}

function gen2()
{
    yield 3;
    yield 4;
}
print_r(iterator_to_array(gen(),false));
// foreach (gen() as $val)
// {
//     echo $val, PHP_EOL;
// }

Closure::call()

<?php
class A {private $x = 1;}

// Pre PHP 7 代码
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, 'A'); // intermediate closure
echo $getX();

// PHP 7+ 代码
$getX = function() {return $this->x;};
echo $getX->call(new A);

define可定义数组常量

<?php
define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // 输出 "cat"
?>

函数新增内容

1、新增参数声明类型(bool,float,int,string)

2、严格模式:declare(strict_types=1);

3、可变参数数量(...$num)

4、新增函数返回值的类型声明

5、可变函数,使用变量保存函数名

6、匿名函数

一般用作一个回调函数参数的值,也可以作为变量的值

7、递归和迭代

新增变化

1、php7版本,字符串中的十六进制字符,不在作为数字。

2、intdiv(x,y) 整除运算符(x除以y)

3、x<=>y 组合比较符 (x等于y,返回0;x大于y,返回1;x小于y,返回-1)

4、三元运算符

$a = (1>2)?'big':'small';

5、变量作用域:局部、全局、静态、参数

函数体内部定义的变量为局部变量,函数体外部定义的变量为全局变量,使用static关键字可以使函数执行完毕局部变量保留,函数定义的参数为参数变量

6、常量定义

define()和const定义

7、预定义常量

__LINK__:文件中的当前行号

__FILE__:文件的完整路径和文件名

__DIR__:文件所在的目录

__FUNCTION__:函数名称

__CLASS__:类的名称

__TRAIT__:trait的名字

__METHOD__:类的方法名

__NAMESPACE__:当前命名空间的名称

php7新增以下常量

PHP_INT_MIN等等

8、while循环

$i = 1;

while($i<=10){

  echo $i++;

}

打印结果:12345678910

9、do while

$i=0;

do{

  $i++;

  echo $i;

}while($i<10);

执行结果:12345678910

10、goto

goto a;

echo 'adad';

a:

echo 'sdsd';

执行结果:sdsd

转载于:https://www.cnblogs.com/xwlong/p/7895964.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值