strict_types

PHP declare 之 strict_types=1

PHP中申明 declare(strict_types=1)的作用:  strict_types=1 及开启严格模式.默认是弱类型校验.具体严格模式和普通模式的区别见下面代码.

Fatal error: Uncaught TypeError: Return value of ChuWu\Christmas\Controller\Index\Index::foo() must be of the type integer, float returned in
<?php
declare(strict_types=1);

namespace ChuWu\Christmas\Controller\Index;
use Magento\Framework\App\Action\Action;

class Index extends Action
{
    /**
     * @inheritDoc
     */
    public function execute()
    {
        // TODO: Implement execute() method.
          echo $q =  $this->foo(2);
          echo $q =   $this->foo(1);
          echo $q =  $this->foo(1.5);
        //foo(1); //返回值类型错误
        //foo(1.5); //参数类型错误
        //foo(2); //正常
    }

     private function foo(int $num): int {
        return $num/2;
    }

}

使用方法:就是在每个php代码中添加到php代码的最开始位置。