PHP警告:get_class()要求参数1为object,给定为null
PHP警告:get_class()要求参数1为object,在第4行的C:\Users\HP\Desktop\test\test.PHP中为空PHP警告:get_class()要求参数1为object,给定为null,php,Php,PHP警告:get_class()要求参数1为object,在第4行的C:\Users\HP\Desktop\test\test.PHP中为空 如何解决此问题?问题可能是,$OldButton可以是null,并且不能对null值调用get\u class。显然,ButtonManager::getButtonForKey($this->UCID,$Name)可以返回null,这就是您的问题所在。您可以通过在第4行之前执行此操作来修复它: public function InitButton($N
如何解决此问题?问题可能是,
$OldButton
可以是null
,并且不能对null
值调用get\u class
。显然,ButtonManager::getButtonForKey($this->UCID,$Name)
可以返回null
,这就是您的问题所在。您可以通过在第4行之前执行此操作来修复它:
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;
}
这意味着
ButtonManager::getButtonForKey($this->UCID,$Name)代码>返回null
。如果这是预期的行为,请将If语句更改为
if (!is_null($OldButton)) {
if(get_class($OldButton) == 'Button') {
...
}
}
这将检查按钮是否为空,然后检查类是否为'button'
如果这不是预期的行为,则说明ButtonManager
$OldButton中出现了错误,$OldButton不是对象。使用var_dump()进行测试。请注意,虽然此代码可以工作,但我建议您检查null
和throw and error,而不是检查notnull
。这使您的代码更具可读性。谢谢。这个问题解决了,如果解决了,请你把它标记为已解决。这样,未来的访问者可以立即找到正确的答案
if ($OldButton && get_class($OldButton) == 'Button')