函数返回值数组和布尔值那么这个函数在php中的返回类型是什么?
我有一个如下的函数。它返回数组值和布尔值,所以我想知道如何定义函数返回类型?此函数的返回类型是什么函数返回值数组和布尔值那么这个函数在php中的返回类型是什么?,php,Php,我有一个如下的函数。它返回数组值和布尔值,所以我想知道如何定义函数返回类型?此函数的返回类型是什么 public function alert($id): ____ //this place value? { $model = Model::find($id); if ($model) { if ($model->status != 1) { return array(
public function alert($id): ____ //this place value?
{
$model = Model::find($id);
if ($model) {
if ($model->status != 1) {
return array(
'header' => 'Failed',
'message' => 'Failed related message'
);
} else {
return array(
'header' => 'Success',
'message' => 'Success related message.'
);
}
}
return false;
}
您可以使用此方法定义返回值:
public function alert($id): ? array
{
$model = Model::find($id);
if ($model) {
if ($model->status != 1) {
return array(
'header' => 'Failed',
'message' => 'Failed related message'
);
} else {
return array(
'header' => 'Success',
'message' => 'Success related message.'
);
}
}
return null;
}
这将导致null或array。php中内置了获取变量类型的功能-
gettype()
。还有settype()
-感谢@mech的回复。你的回答与我的问题有关,我听不懂。我的问题是没有定义任何东西,它工作得很好。但我想知道如何为这种类型的条件定义返回类型?返回false?我不认为这是任何原因,您可以在函数定义@return属性中定义mixed,并在示例中注释它将返回null或array。