在foreach循环中创建的实例是重复的

时间:2017-08-29 11:34:21

标签: php oop

我编写的代码从cURL源读取数据,然后处理它以提取数据记录。

每条记录都成为一个对象的实例,其中包含从数据中设置的多个变量。函数提取所有数据并返回一个对象数组。

问题是所有对象都具有相同的值,这是从源数据中读取的最后一条记录。

经过一些测试后,我意识到这是一个参考问题。从源读取数据,然后将其分配给对象,然后将对象添加到数组中。在循环遍历源中所有记录的循环中重用相同的对象。每当更新此对象时,数组中对象的所有先前值也会重置为最新值,因为它们在更新时继续引用该对象。

如何使所有值独立?

function get_object_array () {
        //reads raw data from cRUL source, returns array of objects

        //array to hold objects
        obj_arr = [];

        //raw data has been split into array called $record, one element for each object
        //loops through $record array
            foreach ($record as $rec) {
                //splits $rec into array of data called $data
                //creates new object, but problem here as this object
                //is being referenced by all values so last value
                //changes all previous objects in array
                $obj = new SaleItem();
                //populates object with record data array
                $obj->set_data($data);
                //add object to array
                $obj_arr [] = $obj;     
            }
        return $obj_arr;
    }

更新:以下是设置数据的功能:

function set_data (array $arr) {
        global $order_num, $name, $price, $cprice, $cheapest, $category;
        try {
            $order_num = (int)$arr[0];
            $name = $arr[1];
            $price = (float)$arr[2];
            $cprice = (float)$arr[3];
            $cheapest = $this->$price <= $this->$cprice ? true : false;
            $category = $arr[5];
            return true;
        }
        catch (Exception $ex) {
            echo $ex;
            return false;
        }   
    }

更新:全班代码:

 class SaleItem {
    public $order_num = 12;
    public $name = "";
    public $price = 3.4;
    public $cprice = 5.6;
    public $cheapest = true;
    public $category = "No Category";

    function set_data (array $arr) {
        try {
            $this->order_num = (int)$arr[0];
            $this->name = $arr[1];
            $this->price = (float)$arr[2];
            $this->cprice = (float)$arr[3];
            $this->cheapest = $price <= $cprice ? true : false;
            $this->category = $arr[5];
            return true;
        }
        catch (Exception $ex) {
            echo $ex;
            return false;
        }   
    }

    function get_data () {
        echo $this->order_num . ' num<br/>';
        echo $this->name . ' name<br/>';
        echo $this->price . ' price<br/>';
        echo $this->cprice . ' cprice<br/>';
        echo $this->cheapest . ' cheapest<br/>';
        echo $this->category . ' category<br/>';
        echo '<br/>';
    }

}//end SaleItem class

1 个答案:

答案 0 :(得分:2)

您正在使用全局变量而不是成员。删除

global $order_num, $name, $price, $cprice, $cheapest, $category;

从函数和前言中使用$ this-&gt;

进行每个作业
$this->order_num = (int)$arr[0];
        $this->name = $arr[1];
        $this->price = (float)$arr[2];
        $this->cprice = (float)$arr[3];
        $this->cheapest = $this->price <= $this->cprice;
        $this->category = $arr[5];