您好,欢迎访问一九零五行业门户网

php7新特性的比较和理解

1.null合并运算符()    语法: 如果变量存在且值不为null,它就会返回自身的值,否则返回它的第二个操作数.
//php7以前 if判断if(empty($_get['param'])) { $param = 1;}else{ $param = $_get['param'];}//php7以前 三元运算符$param = empty($_get['param']) ? 1 : $_get['param'];//php7 null合并运算符$param = $_get['param'] 1;//1
2. define() 定义常量数组//php7以前define("content", "hello world");echo content;//hello world//php7define('animals', [ 'dog', 'cat', 'bird']);echo animals[2];//bird//php7 类外也可使用const来定义常量const constant = 'hello world'; echo constant;//hello world
3. 组合比较符(<=>) 组合比较符用于比较两个表达式.当b时它分别返回-1、0或1. 比较的原则是沿用php的常规比较规则进行的.
//整数echo 1 <=> 1; // 0echo 1 <=> 2; // -1echo 2 <=> 1; // 1//浮点数echo 1.5 <=> 1.5; // 0echo 1.5 <=> 2.5; // -1echo 2.5 <=> 1.5; // 1 //字符串echo "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1
4. 变量类型声明 两种模式: 强制(默认)和严格模式. 可以使用下列类型参数: string,int,float,bool
//... 操作符: 表示这是一个可变参数. php5.6及以上的版本可使用: 函数定义的时候变量前使用.function intsum(int ...$ints){ return array_sum($ints);}var_dump(intsum(2,'3.5'));//5//严格模式//模式声明:declare(strict_types=1); 默认情况值为0,值为1代表为严格校验的模式 declare(strict_types=1);function add(int $a,int $b){ return $a+$b;}var_dump(add(2,'3.5')); //fatal error: uncaught typeerror: argument 2 passed to add() must be of the type integer
5. 返回值类型声明
增加返回类型声明的支持.类似于参数类型声明.(用法在函数定义的后面加 :类型名)
//有效的返回类型declare(strict_types = 1);function getint(int $value): int { return $value;}print(getint(6));//6
//无效返回类型declare(strict_types = 1);function getnoint(int $value): int { return $value+'2.5';}print(getnoint(6));//fatal error: uncaught typeerror: return value of getnoint() must be of the type integer
6. 匿名类 允许new class {} 创建一个匿名的对象.
<?php//php7以前 接口实现interface user{ public function getdiscount();}class vipuser implements user{ //折扣系数 private $discount = 0.6; public function getdiscount() { return $this->discount; }}class goods{ private $price = 200; private $objectvipuser; //user接口vipuser类实现 public function getuserdata(user $user){ $this->objectvipuser = $user; $discount = $this->objectvipuser->getdiscount(); echo "商品价格:".$this->price*$discount; }}$display = new goods();//常规实例化接口实现对象$display->getuserdata(new vipuser);//商品价格:120
<?php//php7 创建一个匿名的对象interface user{ public function getdiscount();}class goods{ private $price = 200; private $objectvipuser; public function getuserdata($user){ $this->objectvipuser = $user; $discount = $this->objectvipuser->getdiscount(); echo "商品价格:".$this->price*$discount; }}$display = new goods();//new匿名对象实现user接口$display->getuserdata(new class implements user{ private $discount = 0.6; public function getdiscount() { return $this->discount; }});//商品价格:120
7. closure::call() closure::call() 方法被添加为一个简短的方式来临时绑定一个对象作用域到一个闭包并调用它. 与php5的bindto相比.它的性能要快得多.
<?php//php7以前class a { private $attribute = 'hello world';}$getclosure = function(){ return $this->attribute;};$getattribute = $getclosure->bindto(new a, 'a');//中间层闭包echo $getattribute();//hello world
<?php//php7class a { private $attribute = 'hello world';}$getclosure = function(){ return $this->attribute;};echo $getclosure->call(new a);//hello world
8. unserialize() unserialize()函数:过滤的特性,可以防止非法数据进行代码注入,提供了更安全的反序列化数据
<?php class a{ public $name = 'admin_a'; } class b{ public $name = 'admin_b'; } $obja = new a(); $objb = new b(); $serializedobja = serialize($obja); $serializedobjb = serialize($objb); //默认行为是接收所有类; 第二个参数可以忽略$dataa = unserialize($serializedobja , ["allowed_classes" => true]); var_dump($dataa);//object(a)#3 (1) { ["name"]=> string(7) "admin_a" }//如果allowed_classes设置为false,unserialize会将所有对象转换为__php_incomplete_class对象 $dataa = unserialize($serializedobja , ["allowed_classes" => false]); var_dump($dataa);//object(__php_incomplete_class)#4 (2) { ["__php_incomplete_class_name"]=> string(1) "a" ["name"]=> string(7) "admin_a" }//转换所有对象到 __php_incomplete_class对象,除了对象"b"$datab = unserialize($serializedobjb , ["allowed_classes" => ["b"]]); var_dump($datab);//object(b)#3 (1) { ["name"]=> string(7) "admin_b" }
9. intlchar intlchar:提供了一些可用于访问unicode字符信息的实用方法的访问. 注意:必须安装intl扩展才能使用!
var_dump(intlchar::codepoint_max);//int(1114111) echo '<br>';var_dump(intlchar::charname('+'));//string(9) "plus sign" echo '<br>';var_dump(intlchar::ispunct('?'));//bool(true)
10. csprng
csprng 函数提供一种简单的机制来生成密码的随机数.
random_bytes() -加密生存被保护的伪随机字符串.
random_int() -加密生存被保护的伪随机整数.
$bytes = random_bytes(8); echo(bin2hex($bytes));//随机2073a110a2e3c497echo '<br>';echo(random_int(1, 999));//随机786echo '<br>';print(random_int(-999, -1));//随机-357
11. use 语句 可以使用单个use语句从相同的命名空间导入类,函数和常量,而不是使用多个use语句.
//php7之前use some\namespace\classa;use some\namespace\classb;use some\namespace\classc as c;use function some\namespace\fn_a;use function some\namespace\fn_b;use function some\namespace\fn_c;use const some\namespace\consta;use const some\namespace\constb;use const some\namespace\constc;// php7之后use some\namespace\{classa, classb, classc as c};use function some\namespace\{fn_a, fn_b, fn_c};use const some\namespace\{consta, constb, constc};
12. intdiv 新增加intdiv()函数,接收两个参数,返回值为第一个参数除于第二个参数的值并取整.
echo intdiv(8,4);//2echo intdiv(10,4);//2echo intdiv(5,10);//0
13. php7 错误处理 php7 改变了大多数错误的报告方式.不同于php5的传统错误报告机制,现在大多数错误被作为error异常抛出.
这种error异常可以像普通异常一样被try / catch块所捕获. 如果没有匹配的try / catch块,则调用异常处理函数(由 set_exception_handler() 注册)进行处理.
如果尚未注册异常处理函数,则按照传统方式处理:被报告为一个致命错误(fatal error).
error类并不是从exception类扩展出来的,所以用catch (exception $e) { ... } 这样的代码是捕获不到error的.你可以用 catch (error $e) { ... } 这样的代码,
或者通过注册异常处理函数( set_exception_handler())来捕获error.
<?php//php7以前 自定义异常处理class getexception extends exception{ public function errormsg(){ return '错误的信息'.$this->getmessage().'<br>错误的代码'.$this->getcode(); }}try { $num =10; if($num > 1) { throw new getexception($num,404); }} catch (getexception $e) { echo $e->errormsg();}
<?php //php7 异常处理try { test();}catch(\exception $e){ echo $e->getmessage();//自定义异常抛出}catch(\error $e) { //系统错误 echo $e->getmessage();//call to undefined function test()}
更多php相关知识,请访问!
以上就是php7新特性的比较和理解的详细内容。
其它类似信息

推荐信息