如何在两个方法中复制属性?


How do I don't duplicate properties over 2 methods?

我有这样的东西

<?php
class Advertising{
    private $param1;
    private $param2;
    public function __construct($param1, $param2){
        $this->param1 = $param1;
        $this->param2 = $param2;
    }
    public function newCustomer($raw_data1, $raw_data2){
        $raw_data1 = mysql_real_escape_string($raw_data1);
        $raw_data2 = mysql_real_escape_string($raw_data2);
        // Now I would use these properties, as it are clean.
    }
    public function editCustomer($raw_data1, $raw_data2){
        $raw_data1 = mysql_real_escape_string($raw_data1);
        $raw_data2 = mysql_real_escape_string($raw_data2);
        // Now I would use these properties, as it are clean.
    }
    public function newStore($raw_data3, $raw_data4){
        $raw_data3 = mysql_real_escape_string($raw_data3);
        $raw_data4 = mysql_real_escape_string($raw_data4);
        // Now I would use these properties, as it are clean.
    }
    public function editStore($raw_data3, $raw_data4){
        $raw_data3 = mysql_real_escape_string($raw_data3);
        $raw_data4 = mysql_real_escape_string($raw_data4);
        // Now I would use these properties, as it are clean.
    }
    // As you can see here, there's 2 pair of methods. The one that creates a new element, and the another that modify it. There's common properties each other, but not with the another methods.
}

我不能通过__construct传递这些属性,如$raw_data1和$raw_data2,因为它用于处理所有通用代码,而不是单个特定的方法,在这种情况下

您的意思是希望newXxx函数将类中的数据保存为属性,以便editXxx函数可以更改它们吗?

如果有,这对你有帮助吗?

class Advertising{
    private $param1;
    private $param2;
    public $Customer = NULL;
    public function __construct($param1, $param2){
        $this->param1 = $param1;
        $this->param2 = $param2;
    }
    public function newCustomer($raw_data1, $raw_data2){
        $this->Customer = new Customer();
        $this->Customer->data1 = mysql_real_escape_string($raw_data1);
        $this->Customer->data2 = mysql_real_escape_string($raw_data2);
        // Now I would use these properties, as it are clean.
    }
    public function editCustomer($new_data1, $new_data2){
        if ( isset($this->Customer) ) {
            $this->Customer->data1 = mysql_real_escape_string($new_data1);
            $this->Customer->data2 = mysql_real_escape_string($new_data2);
        } else {
            // report error
        }
        // Now I would use these properties, as it are clean.
    }