函数返回的PHP null值被解释为$this

函数返回的PHP null值被解释为$this,php,Php,也许我有点累了,我错过了一些东西,但到目前为止,这是我用PHP遇到的最奇怪的问题,我会尽力以最好的方式解释它 在类Compiler\Bids中,我有一个compileData函数: namespace Compiler; class Bids extends CompilerItem { protected $service; public function compileData() { foreach($bids as $bid)

也许我有点累了,我错过了一些东西,但到目前为止,这是我用PHP遇到的最奇怪的问题,我会尽力以最好的方式解释它

在类
Compiler\Bids
中,我有一个
compileData
函数:

namespace Compiler;

class Bids extends CompilerItem
{
    protected $service;

    public function compileData()
    {
        foreach($bids as $bid)
        {
            $buyerGroup = $this->service->serviceProperty->whereEqual('id', 'idgoeshere');
            if($buyerGroup || $buyerGroup === null)
            {
                echo 'BuyerGroup class: '.get_class($buyerGroup).'<br>';
            }
        }
    }
}
发生的情况如下:
compileData
函数中的
echo
语句运行并产生以下输出:

BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: Compiler\Bids
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: Compiler\Bids
BuyerGroup class: BuyerGroup
BuyerGroup class: Compiler\Bids
如您所见,当
whereEqual
函数中的条件未满足时,将返回
null
,但在
compileData
函数中,这被解释为
$this

如果我将
whereEqual
函数修改为只返回
当不满足条件时,输出如下:

BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
这是正确的

为什么PHP会以这种方式运行?我遗漏了什么吗?

当在该上下文中没有参数(相当于空参数)调用时,
get_class()
函数返回当前类的名称(从类中调用时),从而解释您看到的行为

谢谢你的时间:)我对自己说:当你累了的时候,千万不要编程,因为你可能会浪费你的时间,别人的时间,你会在stackoverflow上获得否决票,很容易忘记php中的一个奇怪之处。很抱歉那些讨厌的反对票,我明天会把你的问题投上去。
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup
BuyerGroup class: BuyerGroup