php7 新内容

一、use加强php

以thinkphp5.0为例thinkphp

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

二、匿名类php7

<?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 增强app

<?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可定义数组常量thinkphp5

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

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

函数新增内容this

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

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

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

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

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

六、匿名函数

通常用做一个回调函数参数的值,也能够做为变量的值

七、递归和迭代

新增变化

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

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

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

四、三元运算符

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

五、变量做用域:局部、全局、静态、参数

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

六、常量定义

define()和const定义

七、预约义常量

__LINK__:文件中的当前行号

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

__DIR__:文件所在的目录

__FUNCTION__:函数名称

__CLASS__:类的名称

__TRAIT__:trait的名字

__METHOD__:类的方法名

__NAMESPACE__:当前命名空间的名称

php7新增如下常量

PHP_INT_MIN等等

八、while循环

$i = 1;

while($i<=10){

  echo $i++;

}

打印结果:12345678910

九、do while

$i=0;

do{

  $i++;

  echo $i;

}while($i<10);

执行结果:12345678910

十、goto

goto a;

echo 'adad';

a:

echo 'sdsd';

执行结果:sdsd

相关文章
相关标签/搜索