Php 如何返回变量而不是字符串?
在本例中,函数validateJsonNotEmptyImage未按预期工作 我只是想澄清一下Php 如何返回变量而不是字符串?,php,laravel,Php,Laravel,在本例中,函数validateJsonNotEmptyImage未按预期工作 我只是想澄清一下 'src' => $image->src, // WORKS 'height' => $this->validateJsonNotEmptyImage('type', 'width'), // DOES NOT WORK 我看到两个错误 1您的方法validateJsonNotEmptyImage,在主体中,如果它应该用null[here1]检查,您就错了,并且您应该返回值而
'src' => $image->src, // WORKS
'height' => $this->validateJsonNotEmptyImage('type', 'width'), // DOES NOT WORK
我看到两个错误
1您的方法validateJsonNotEmptyImage,在主体中,如果它应该用null[here1]检查,您就错了,并且您应该返回值而不是字符串[here2]。
而且每次都应该返回任何值或null[here3]
public function validateJsonNotEmptyImage($type, $array1) {
if ($type != null && $array1 != null) { <----------- here1
if (isset($type->$array1)) {
return $type->$array1; <----------- here2
} else {
return null;
}
}
return null; <----------- here3
}
您应该删除$image上的单引号
$productsImagesToSiteArray = array();
foreach ($this->resource->images as $image) {
$productsImagesToSiteArray[] = array(
'height' => $this->validateJsonNotEmptyImage('$image', 'width'),
'src' => $image->src,
);
}
public function validateJsonNotEmptyImage($type, $array1) {
if ($type != null && $array1 != null) { <----------- here1
if (isset($type->$array1)) {
return $type->$array1; <----------- here2
} else {
return null;
}
}
return null; <----------- here3
}
$this->validateJsonNotEmptyImage($image, 'width')