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