php指针 2.标量类型和返回值类型声明
发布时间:2022-12-23 11:21:04 所属栏目:PHP教程 来源:
导读:、 declare(strict_types=1); //strict_types=1表示严格模式
function sum(int ...$ints){
return array_sum($ints);
}
var_dump(sum(1,'2','3.1',4.1));
执行结果
function sum(int ...$ints){
return array_sum($ints);
}
var_dump(sum(1,'2','3.1',4.1));
执行结果
|
、 declare(strict_types=1); //strict_types=1表示严格模式 function sum(int ...$ints){ return array_sum($ints); } var_dump(sum(1,'2','3.1',4.1)); 执行结果 Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given declare(strict_types=1); function sum(int ...$ints):int { return array_sum($ints); } var_dump(sum(1,2,3)); //6 php7.1对函数返回值类型作了扩充,可定义返回值类型为void,不管是否开启严格模式,==只要函数有"return;" 之外的其它return语句都会报错==数组 declare(strict_types=1); function sum(int ...$ints):void { // return NULL; //报错 return ; //后面有 "return;"以外的语句也报错 // return array_sum($ints); } var_dump(sum(1,2,3)); php7.1对参数类型和返回值类型还有进一步的支持,其类型可使空类型,在参数或者返回值类型声明前加上"?"表示要么是null,要么是声明的类型php7 declare(strict_types=1); function test(?int $a):?int { return $a; } var_dump(test(null)); //NULL var_dump(test(1)); //int(1) var_dump(test('a')); // Fatal error 3.null合并操做符 $page=isset($_GET['p'])?$_GET['p']:0; //php7 $page =$_GET['p'] ??0; //三元运算符 $page =$_GET['p'] ?? $_POST['p'] ??0; 4.常量数组 php7以前没法经过define来定义一个常量数组的php指针,php7支持了这个操做闭包 define('STUDENT',['boy','girl']); print_r(STUDENT); 5.namespace批量导入 //php以前 use Space\ClassA; use Space\ClassB; use Space\ClassC C; //php7 use Space\{ClassA,ClassB,ClassC as C}; 6.throwable接口 try{ abc(); }catch (Error $e){ print_r($e); } echo 123; php5里 Fatal error ,当即退出 以后的代码不会执行了 php7里 Error Object ( [message:protected] => Call to undefined function abc() [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/hgfs/www/web/md.php [line:protected] => 4 [trace:Error:private] => Array ( ) [previous:Error:private] => ) 123 或者经过注册异常处理函数来处理 set_exception_handler(function($e){ echo "err:".print_r($e,true); }); abc(); echo 123; php7下结果,php5依然Fatal error指针 err:Error Object ( [message:protected] => Call to undefined function abc() [string:Error:private] => [code:protected] => 0 [file:protected] => /mnt/hgfs/www/web/md.php [line:protected] => 7 [trace:Error:private] => Array ( ) [previous:Error:private] => ) 以后的也不会执行了 7.Closure::call() 在php7以前,当动态的给一个对象添加方法时,能够经过Closure来复制一个闭包对象,并绑定到一个$this对象和类做用域 class People{ private $age=10; } $f=function(){ return $this->age+1; }; $p=$f->bindTo(new People,'People'); echo $p(); 在php7可经过call来暂时绑定一个闭包对象到$this对象并调用它 class People{ private $age=10; } $f=function(){ return $this->age+1; }; echo $f->call(new People); 8.intdiv echo intdiv(10,3); 9.list方括号写法 $a=[1,2,3]; list($n1,$n2,$n3)=$a; //php7 [$n1,$n2,$n3]=[4,5,6]; //[]并非数组,而是list的简略形式 10.foreach遍历数组再也不修改内部指针 $arr=[1,2,3,4,5,6]; foreach ($arr as $key => $value) { if($value ==2) break; } echo current($arr);//php7下 1,php5下 3 11.匿名类可使用 new class来定义,匿名类可使用来代替完整的定义 interface Cache{ public function read (); } class Ca { private $cache; public function setcache(Cache $cache){ $this->cache=$cache; } } $fcache =new Ca; $fcache->setcache(new Class implements Cache { public function read(){ } }); 其它,移除ASP和script PHP标签,移除$HTTP_RAW_POST_DATA、匿名类、常量类可见性等code (编辑:百客网 - 百科网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐

