PHP警告:get_class()期望参数1为对象,给定null

时间:2020-04-30 16:41:37

标签: php

public function InitButton($Name, $Group, $T=0, $L=0, $W=1, $H=1, $BStyle=null, $Text='', $Expire = 0, $Repeat=true)
{
    $OldButton = ButtonManager::getButtonForKey($this->UCID, $Name);

    //Line 4 below: 
    if(get_class($OldButton) == 'Button') {

        $Button = $OldButton;
    } else {
        $Button = new Button($this->UCID, $Name, $Group);
    }
    $Button->T($T)->L($L)->W($W)->H($H);
    $Button->BStyle($BStyle);
    if(is_array($Text)) {
        $Button->Text($Text[0]);
        $this->bTexts[$Name] = $Text;
    } else {
        $Button->Text($Text);
        $this->bTexts[$Name][0] = $Text;
    }
    $Button->Send();

    $this->bState[$Name]['ID'] = 0;
    $this->bState[$Name]['timestamp'] = time() - 1;
    $this->bState[$Name]['override'] = false;
    if($Expire > 0) {
        $this->bState[$Name]['expire'] = time() + $Expire;
    } else {
        $this->bState[$Name]['expire'] = -1;
    }
    $this->bState[$Name]['repeatText'] = $Repeat;

}

PHP警告:get_class()期望参数1为对象,在第4行的C:\ Users \ HP \ Desktop \ test \ test.php中给出空值

我该如何解决?

2 个答案:

答案 0 :(得分:1)

这意味着ButtonManager::getButtonForKey($this->UCID, $Name);返回null。如果这是预期的行为,则可以将if语句更改为

if ($OldButton && get_class($OldButton) == 'Button')

这将检查按钮是否不为null,然后检查类是否为'Button'

如果这不是预期的行为,则说明ButtonManager中有问题

答案 1 :(得分:0)

问题可能是$OldButton可以是null,而您不能在get_class值上调用null。显然,ButtonManager::getButtonForKey($this->UCID, $Name)可以返回null,这就是您的问题。您可以通过在第4行之前执行此操作来修复它:

if (!is_null($OldButton)) {
    if(get_class($OldButton) == 'Button') {
        ...
    }
}