2016-05-15 82 views
0

我有我是PHP中的nubie。 我有一个问题的打击。如何解决未定义的属性

Notice on line 328 in file modules/swipe/swipe.php 
[8] Undefined property: Swipe::$_postErrors 




    public function getContent() { 
     $this->_html = '<img src="'.$this->_p otocol.$this->context->shop->domain.$this->_path.'logo-big.png" style="margin-bottom:20px" />'; 
     if (Tools::isSubmit('btnSubmit')) { 
      $this->_postValidation(); 
      if (!count($this->_postErrors)) **((Line 328))** 
       $this->_postProcess(); 
      else 
       foreach ($this->_postErrors as $err) 
        $this->_html .= '<div class="alert error">' . $err . '</div>'; 
     } 
     else 
      $this->_html .= '<br />'; 

     $this->_displayTop(); 
     $this->_displayForm(); 

     return $this->_html; 
    } 
} 

你能帮我解决这个问题吗? 我附上了swipe.php文件。

谢谢。

+0

只要声明它。 'private $ _postErrors = array();' –

+0

谢谢。这是Vp_arth最简单的方法。 – HappyDayToday

回答

1

当你访问属性&属性不存在此错误时更改您的代码此

public function getContent() { 
     $this->_html = '<img src="'.$this->_p otocol.$this->context->shop->domain.$this->_path.'logo-big.png" style="margin-bottom:20px" />'; 
     if (Tools::isSubmit('btnSubmit')) { 
      $this->_postValidation(); 
      if (isset($this->_postErrors) 
      { 
      if (!count($this->_postErrors)) 
       $this->_postProcess(); 
      else 
       foreach ($this->_postErrors as $err) 
        $this->_html .= '<div class="alert error">' . $err . '</div>'; 
      } 
     } 
     else 
      $this->_html .= '<br />'; 

     $this->_displayTop(); 
     $this->_displayForm(); 

     return $this->_html; 
    } 
    } 

。在生产中,这个错误不会显示,因为您不会在生产中显示任何错误,但是在开发过程中显示所有错误是一种很好的做法。

使用isset函数检查属性是否存在。

0

更改,如果条件

if (empty($this->_postErrors)) 
    $this->_postProcess(); 

这样,你的条件也将在个案工作$this->_postErrors是不确定的。