PHP isset在非空变量上返回false
这里有一种情况,我必须使用PHP isset在非空变量上返回false,php,laravel,isset,Php,Laravel,Isset,这里有一种情况,我必须使用isset(),它返回false,即使变量中有值。我的PHP版本如下: PHP 7.0.33-14+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:55:39) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend
isset()
,它返回false
,即使变量中有值。我的PHP
版本如下:
PHP 7.0.33-14+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:55:39) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-14+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
"laravel/framework": "5.5.28",
我的laravel
版本如下:
PHP 7.0.33-14+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:55:39) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-14+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
"laravel/framework": "5.5.28",
下面是一段代码,其中包含isset()
:
return isset($this->pivot->factor) ? $this->pivot->factor : $this->getFactor();
现在如果我这样做:
\Log::debug('$this->pivot->factor', [$this->pivot->factor]);
变量$this->pivot->factor
中的值是11
,这是正确的,但是如果我执行dd(isset($this->pivot->factor))
,则返回false
。这是怎么回事?如何解决这个问题
编辑
方法如下:
public function getLinkedFactor()
{
if ($this->priceResponce) {
$rateAdditionals = $this->priceResponce->rate->additionals;
$rateAdditional = $rateAdditionals->filter(function ($item) {
return $item->id == $this->id;
})->first();
if ($rateAdditional && $rateAdditional->pivot->factor) {
return $rateAdditional->pivot->factor;
}
}
\Log::debug('$this->pivot->factor', [$this->pivot->factor]);
return isset($this->pivot->factor) ? $this->pivot->factor : $this->getFactor();
}
这是访问该代码的测试:
/**
* @test
*/
public function shouldConsiderLinkedFactor()
{
$officeAttrs = [
'id' => 1,
'additional_id' => 2,
'office_id' => 3,
'factor' => 11
];
$officeAdditional = $this->makeEloquentMock(OfficeAdditional::class, $officeAttrs);
$officeAdditional->shouldReceive('hasGetMutator')->andReturn(true);
$officeAdditional->shouldReceive('getAttributeValue')->andReturn($officeAttrs['factor']);
$officeAdditional->shouldReceive('toJson');
$attributes = [
'additional_type_id' => 2,
'factor' => 10,
'name' => 'test fixed additional',
'description' => 'asdasdasd',
'has_input' => false,
'kind' => 'linked',
'input_label' => '',
'carrier_facing_cost' => 1,
'min' => 5,
'max' => 10
];
$additional = Additional::make($attributes);
$additional->pivot = $officeAdditional;
$this->assertEquals($officeAttrs['factor'], $additional->getLinkedFactor());
}
我没有50分可以加入上面的评论,所以我希望这是一个足够的答案 更新:将getLinkedFactor的最后一行替换为
return
$this->whenPivotLoaded('pivot_table_name', function () {
return $this->pivot->factor;
})
??
$this->getFactor();
我找到了一个适合我这种情况的工作。我替换了这一行:
return isset($this->pivot->factor) ? $this->pivot->factor : $this->getFactor();
为此:
return $this->pivot === null ? $this->getFactor() : $this->pivot->factor === null ? $this->getFactor() : $this->pivot->factor;
这样我就不必使用
isset()
显示代码的上下文,这里的信息太少,无法进行故障排除。您是否在完全相同的位置执行这两个代码?请给我们看完整的代码。什么是pivot
?什么是系数
?不要只是返回它-在读取值之前和之后添加正确的调试语句。您在模型中的何处运行此代码?当类定义magic Getter并且未能实现\u isset
时,会发生这种情况。你能解释一下这和我的情况有什么关系吗?@Saani你在使用资源吗?根据您上面的评论,您似乎在使用模型,而不是资源,对吗?@lagbox yes!我使用的是Model。@Saani,很遗憾,这个特定的答案不适用