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

问题描述 投票:0回答:2
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为object,在C:\Users\HP\Desktop\test\test.php的第4行给出的参数为空。

我如何解决这个问题?

php
2个回答
1
投票

这意味着 ButtonManager::getButtonForKey($this->UCID, $Name); 返回 null. 如果这是预期的行为,也许可以将if-statement改为

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

这将检查按钮是否为空,然后如果该类是 'Button'

如果这不是预期的行为,那么就说明在以下方面出了问题 ButtonManager


0
投票

问题可能在于 $OldButton 可以 null你不能叫 get_class 关于 null 价值。显然,。ButtonManager::getButtonForKey($this->UCID, $Name) 可以返回 null这就是你的问题。你可以通过在第4行之前这样做来解决它。

if (!is_null($OldButton)) {
    if(get_class($OldButton) == 'Button') {
        ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.