## 原由
最近写了一个新的项目,在测试的时候发现PHP报了一个很诡异的错误
```
Argument 1 passed to xxx must be an instance of string, string given, called in yyy
```
## 分析
一开始看到这个错误,我第一反应就是是不是把`string`写错了,然后认真看了下,这完全一毛一样啊!
![E6FB8C7BCFA3AC04E9AFE00BE369D6FD.jpg](https://storage.liesauer.net/2018/10/2215039208.jpg)
然后就到百度搜索了一波,找到了一篇sf的问答,说是5.x版本只支持具体的`object`类型以及`array`,然后翻了一下PHP手册,还真是。
| Type | Description | Minimum PHP version |
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- |
| Class/interface name | The parameter must be an instanceof the given class or interface name. | PHP 5.0.0 |
| self | The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. | PHP 5.0.0 |
| array | The parameter must be an array. | PHP 5.1.0 |
| callable | The parameter must be a valid callable. | PHP 5.4.0 |
| bool | The parameter must be a boolean value. | PHP 7.0.0 |
| float | The parameter must be a floating point number. | PHP 7.0.0 |
| int | The parameter must be an integer. | PHP 7.0.0 |
| string | The parameter must be a string. | PHP 7.0.0 |
| iterable | The parameter must be either an array or an instanceof Traversable. | PHP 7.1.0 |
| object | The parameter must be an object. | PHP 7.2.0 |
## 解决
不使用类型限定,或者可以选择升级PHP版本。
## 总结
果然是进步容易,倒退难啊,平时一直写的是`7.0`的,现在换成了`5.6`。