你能检查我的PHP代码吗?(Opencart扩展)

imzjd6km  于 2022-12-17  发布在  PHP
关注(0)|答案(1)|浏览(112)

我正在使用一个插件在opencart中获得支付。但是我认为PHP代码中有一些错误。
示例:PHP警告:为foreach()提供的参数无效

<?php
***************************
***************************
***************************
    private $category_installment = array();
    private $category_full;
    public function __construct()
    {
        $this->hash = new Hash();
    }
***************************
***************************
***************************
    protected function categorySearch($category_id = 0)
    {
        if (!empty($this->category_full[$category_id]) and array_key_exists($this->category_full[$category_id], $this->category_installment)) {
            $return = $this->category_installment[$this->category_full[$category_id]];
        } else {
            foreach ($this->category_full as $id => $parent) {
                if ($category_id == $id) {
                    if ($parent == 0) {
                        $return = 0;
                    } elseif (array_key_exists($parent, $this->category_installment)) {
                        $return = $this->category_installment[$parent];
                    } else {
                        $return = $this->categorySearch($parent);
                    }
                } else {
                    $return = 0;
                }
            }
        }
        return $return;
    }
}

你能帮我找到代码中的错误吗?(Opencart扩展)

k3bvogb1

k3bvogb11#

循环前检查数据类型,使用:获取类型();

<?php
***************************
***************************
***************************
    private $category_installment = array();
    private $category_full;
    public function __construct()
    {
        $this->hash = new Hash();
    }
***************************
***************************
***************************
    protected function categorySearch($category_id = 0)
    {
        if (!empty($this->category_full[$category_id]) and array_key_exists($this->category_full[$category_id], $this->category_installment)) {
            $return = $this->category_installment[$this->category_full[$category_id]];
        } else {
            if(!empty($this->category_full){  /* added this*/
                foreach ($this->category_full as $id => $parent) {
                    if ($category_id == $id) {
                        if ($parent == 0) {
                            $return = 0;
                        } elseif (array_key_exists($parent, $this->category_installment)) {
                            $return = $this->category_installment[$parent];
                        } else {
                            $return = $this->categorySearch($parent);
                        }
                    } else {
                        $return = 0;
                    }
                }
            } else {  /* added this*/
                $return = 0;/* added this*/
            } /* added this*/
        }
        
        return $return;
    }
}

相关问题