php嚴格模式的使用


<?php
declare (strict_types = 1);
namespace app\controller;

use app\BaseController;
use think\facade\Db;

class Index extends BaseController
{
    public function index()
    {
        $this->hello((int) $_GET['a']);
        $a = Db::name('user')->where('id', 1)->find();

        return json($a);
    }

    public function hello(string $name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
}

1.declare (strict_types = 1); //開啟嚴格模式,檢查參數的類型

Argument 1 passed to app\controller\Index::hello() must be of the type string, int given, called in /www/wwwroot/test/tp6/app/controller/Index.php on line 12

1.傳參和定義的類型不一致會直接報錯,去掉嚴格模式不會報錯

2.hello(string $name = 'ThinkPHP6') //hello函數定義name必須傳字符串類型

3.$this->hello((int) $_GET['a']); //實際傳參時候給了一個整形

4.declare (strict_types = 1); 在嚴格模式下直接報錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM