<?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); 在严格模式下直接报错