简体   繁体   中英

How to solve Undefined property

I have I am a nubie in PHP. I'm having an issue blow.

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;
    }
}

Can you please help me to resolve the issue? I attached the swipe.php file.

Thank you.

Change your code to this

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;
    }
    }

When you are accessing a property & the property doesn't exists this error occurs. In production this error will not show as you will not show any errors in production but in development it's good practice to show all errors.

Using isset function you are checking if the property does exist.

Change your if condition to

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

That way your condition will also work in case $this->_postErrors is undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM