refactor common code used by Models

I have several models using the same code and I would like to handle it better.





class Expense extends \yii\db\ActiveRecord

{

    const STATUS_DELETED = 0;

    const STATUS_NEW = 1;

    const STATUS_PENDING = 2;

    const STATUS_APPROVED = 3;

    const STATUS_DECLINED = 4;

    

    public static function StatusInText()

    {

        return array(

            self::STATUS_DELETED => 'DELETED', 

            self::STATUS_NEW => 'NEW', 

            self::STATUS_PENDING => 'PENDING', 

            self::STATUS_APPROVED => 'APPROVED', 

            self::STATUS_DECLINED => 'DECLINED', 

        );

    }

    

    public function StatusToText()

    {

        $status = self::StatusInText();

        if (array_key_exists($this->status, $status)) {

            return $status[$this->status];

        } else {

            return '';

        }

    }







class Car extends \yii\db\ActiveRecord

{

    const STATUS_DELETED = 0;

    const STATUS_NEW = 1;

    const STATUS_PENDING = 2;

    const STATUS_APPROVED = 3;

    const STATUS_DECLINED = 4;

    

    public static function StatusInText()

    {

        return array(

            self::STATUS_DELETED => 'DELETED', 

            self::STATUS_NEW => 'NEW', 

            self::STATUS_PENDING => 'PENDING', 

            self::STATUS_APPROVED => 'APPROVED', 

            self::STATUS_DECLINED => 'DECLINED', 

        );

    }

    

    public function StatusToText()

    {

        $status = self::StatusInText();

        if (array_key_exists($this->status, $status)) {

            return $status[$this->status];

        } else {

            return '';

        }

    }



As you see, both the model use same constant and same public function. It’s duplication. How can I improve the code ?

any ideas ?

Hey,

You could write your own BaseModel containing the common code.

Then extend from that BaseModel.

Regards

are you thinking like




class BaseModel extends \yii\db\ActiveRecord

{

    const STATUS_DELETED = 0;

    const STATUS_NEW = 1;

    const STATUS_PENDING = 2;

    const STATUS_APPROVED = 3;

    const STATUS_DECLINED = 4;

    

    public static function StatusInText()

    {

        return array(

            self::STATUS_DELETED => 'DELETED', 

            self::STATUS_NEW => 'NEW', 

            self::STATUS_PENDING => 'PENDING', 

            self::STATUS_APPROVED => 'APPROVED', 

            self::STATUS_DECLINED => 'DECLINED', 

        );

    }

    

    public function StatusToText()

    {

        $status = self::StatusInText();

        if (array_key_exists($this->status, $status)) {

            return $status[$this->status];

        } else {

            return '';

        }

    }


class Expense extend BaseModel {


}



dont you think a trait is a better option ?





trait CommonTrait {

    const STATUS_DELETED = 0;

    const STATUS_NEW = 1;

    const STATUS_PENDING = 2;

    const STATUS_APPROVED = 3;

    const STATUS_DECLINED = 4;

    

    public static function StatusInText()

    {

        return array(

            self::STATUS_DELETED => 'DELETED', 

            self::STATUS_NEW => 'NEW', 

            self::STATUS_PENDING => 'PENDING', 

            self::STATUS_APPROVED => 'APPROVED', 

            self::STATUS_DECLINED => 'DECLINED', 

        );

    }

    

    public function StatusToText()

    {

        $status = self::StatusInText();

        if (array_key_exists($this->status, $status)) {

            return $status[$this->status];

        } else {

            return '';

        }

    }

}


class Expense extends \yii\db\ActiveRecord {

    use CommonTrait ;

    /* ... */

}




Hey,

Was just one example.

Of course you could also use trait…

Or any other solution that helps you to reach your target. ;)

Regards

Hi,

you maybe intrested in the extension I wrote to help manage the life cycle of a model inside a workflow … Have a look and see if it can help you.

here it is : yii2-workflow

ciao

B)