这里有新鲜出炉的 PHP 教程,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
本文通过实例给大家介绍了 php7 新特性的相关知识,需要的朋友参考下吧
类型声明
看代码,一目了然了。
- class person
- {
- public function age(int $age) : string
- {
- return 'Age is ' . $age;
- }
- }
命名空间与 use 关键字批量声明
非混合模式
- use Publishers\Packt\{ Book, Ebook, Video};
- use function Publishers\Packt\{ getBook, saveBook };
- use const Publishers\Packt\{ COUNT, KEY };
混合模式
- use Publishers\Packt\ {
- Book,
- Ebook,
- Video,
- function getBook,
- function saveBook,
- const COUNT,
- const KEY
- };
复合模式
- use Publishers\Packt\ {
- Paper\Book,
- Electronic\Ebook,
- Media\Video
- };
匿名类
匿名类的声明与使用时同时进行的,具备其他类所具备的所以功能,区别在于匿名类没有类名。语法如下:
- new class(argument) { definition };
匿名类是没有类名的,但在 PHP 内部,会在内存的引用地址表中为其分配一个全局唯一的名称。
- $name = new class('You') {
- public
- function __construct($name) {
- echo $name;
- }
- };
匿名类可以继承父类及父类的方法。
- class Packt
- {
- protected $number;
- public function __construct()
- {
- echo 'parent construct';
- }
- public function getNumber() : float
- {
- return $this->number;
- }
- }
- $number = new class(5) extends Packt
- {
- public function __construct(float $number)
- {
- parent::__construct();
- $this->number = $number;
- }
- };
- echo $number->getNumber();
匿名类可以继承接口。
- interface Publishers {
- public
- function __construct(string name, string address);
- public
- function getName();
- public
- function getAddress();
- }
- class packt {
- protected $number;
- protected $name;
- protected $address;
- public
- function...
- }
- $info = new class('name', 'address') extends Packt implement Publishers {
- public
- function __construct(string $name, string $address) {
- $this - >name = $name;
- $this - >address = $address;
- }
- public
- function getName() : string {
- return $this - >name;
- }
- public
- function getAddress() : string {
- return $this - >address;
- }
- }
- echo $info - >getName().' '.$info - >getAddress();
匿名类可以嵌套在一个类中使用。
- class Math
- {
- public $first_number = 10;
- public $second_number = 10;
- public function add() : float
- {
- return $this->first_number + $this->second_number;
- }
- public function mutiply_sum()
- {
- return new class() extends Math
- {
- public function mutiply(float $third_number) : float
- {
- return $this->add() * $third_number;
- }
- };
- }
- }
- $math = new Math();
- echo $math->mutiply_sum()->mutiply(2);
摒弃老式构造函数
从 PHP4 开始,构造函数可以通过命名与类名一致的方式来声明自己是构造函数,在 PHP7 中这种方式声明构造函数依然可以使用,但不推荐使用,会输出不推荐的信息 Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Packt has a deprecated constructor in ...,PHP7 中推荐使用 __construct()。
throwable 接口
从 PHP7 开始,程序中的 fatal 错误都可以被截获,PHP7 提供了 throwable 接口,异常与错误都继承于这个接口。
Error
现在大多数的 fatal 错误情况会抛出一个 error 实例,类似于截获异常,error 实例可以被 try/catch 截获。
- try
- {
- ...
- } catch(Error $e)
- {
- echo $e->getMessage();
- }
一些错误情况只有 error 的子实例会被抛出,例如 TypeError、DivisionByZeroError、ParseError 等。
<=> 操作符
<=> 操作符将 ==、<、> 三个比较操作符打包在了一起,具体使用规则如下。
操作符两边相等时返回 0
操作符左边小于右边时返回 -1
操作符左边大于右边时返回 1
null 合并运算符
合并运算符,在第一操作数存在时可被直接返回,否则返回第二操作数。
- $title = $post['title'] ? ?NULL;
- $title = $post['title'] ? ?$get['title'] ? ?'No title';
uniform 变量语法
- $first = ['name' => 'second'];
- $second = 'two';
- echo $$first['name'];
- echo ${Sfirst['name']}; // PHP7
- ...
- echo $object->$methods['title'];
- echo $object->{$methods['title']}; // PHP7
主要是因为 PHP7 与之前版本 PHP 的解析方式不一样,在 PHP7 中加上花括号就可以啦,就像上边代码这样,否则会报错。
常量数组
从 PHP5.6 开始常量数组可以用 const 关键字来声明,在 PHP7 中常量数组可以通过 define 函数来初始化。
- const STORES = ['en', 'fr', 'ar']; // php5.6
- define('STORES', ['en', 'fr', 'ar']); // php7
switch 中的 default 默认值
在 PHP7 之前,switch 语句中允许多个 default 默认值,从 PHP7 开始,只能有一个 default 默认值,否则会产生 fatal 级别错误。
- // php7之前
- switch (true) {
- case 'value':
- #code...
- break;
- default:
- #code...
- break;
- default:
- #code...
- break;
- }
- // php7
- switch (true) {
- case 'value':
- #code...
- break;
- default:
- #code...
- break;
- }
session_start 函数中的选项数组
在 PHP7 之前,使用 session 的时候都必须先调用 session_start() 函数,且这个函数并没有参数需要传递,所有 session 相关的配置都在 php.ini 文件中,从 PHP7 开始,可以在调用这个函数时传递参数选项数组,这些设置信息将覆盖 php.ini 中的 session 配置。
- session_start([
- 'cookie_lifetime' => 3600,
- 'read_and_close' => true
- ]);
unserialize 函数引入过滤器
unserialize() 可以反序列化任何类型的对象,没有任何过滤项,不安全,PHP7 在 unserialize() 中引入了过滤器,且默认允许反序列化所有类型的对象。
- $result = unserialize($object, ['allowed_classes' = >['Book', 'Ebook']]);
来源: http://www.phperz.com/article/17/0806/338757.html