我如何不重复2种方法的属性?

时间:2014-05-29 11:08:02

标签: php oop

我有类似的东西

<?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.
}

我无法将这些属性(例如$ raw_data1和$ raw_data2)传递给__construct,因为它在这种情况下用于处理所有常用代码,而不是单个和特定方法。

1 个答案:

答案 0 :(得分:0)

您是否希望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.
    }