<?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); 在嚴格模式下直接報錯