PHP类方法将参数视为对象而不是字符串/动态解释参数数据类型


PHP class method treats parameter as an object instead of string/dynamically interpreting the parameter data type

我今天发现了一些新东西。

我有一个PHP类的典型成员,private, public和protected方法。

方法之一是:

protected function processThis($dataString)
{
   $dataStringJson = json_decode($dataString);
}

然后输出一个警告:

json_decode()期望参数1是字符串,对象在…/File.php在行xxx

等等,PHP不是松散类型和动态解释吗?

当然是,但是在某些函数中,最好提醒人们他们正在做一些奇怪的事情。你也会得到$f = "1"; array_shift($f);的警告。

如果你想让json_decode工作,那么转换为字符串是很容易的:

protected function processThis($dataString)
{
   $dataStringJson = json_decode(''.$dataString);
}