首页 > Web开发 > 详细

php严格模式的使用

时间:2021-06-17 17:07:41      阅读:32      评论:0      收藏:0      [点我收藏+]
<?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); 在严格模式下直接报错

php严格模式的使用

原文:https://www.cnblogs.com/hxmbk/p/14893298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!