settingCalculated variable in Model and access

setting a Calculated variable in Model

I have a calculated variable defined in Dairies model - public $sexLabel;

Also have functions to retieve sex of dairy which works as below

public static function getSexLabels()


{


    return [


        self::MALE => 'Bull',


        self::FEMALE => 'Heifer',


    ];


}	 


 


public function getSexLabel()


{


    $lables = self::getSexLabels();


   


	if (isset($lables[$this->sex])) {


		$this->sexLabel = $lables[$this->sex];


		//echo 'sexLabel: '.$this->sexLabel."<br>";


		return $lables[$this->sex];


    } else {


        return "invalid sex ({$this->sex})";


    }


}	 

problem is $sexLabel variable is not available in views and so i have to call the function getSexLabel().

Is their a way to set $sexLabel so i can retrieve in view as $model->$sexLabel, Or is their a function that can be called initialise calculated variables

Thanks

Working example:

Model:




/**

     * @param bool $colored

     * @return array

     */

    public static function getNdsLabels($colored = false)

    {

        return [

            self::WITH_NDS => ($colored ? '<span style="color: #5cb85c">с НДС</span>' : 'с НДС'),

            self::WITHOUT_NDS => ($colored ? '<span style="color: #eea236">без НДС</span>' : 'без НДС'),

        ];

    }


    /**

     * @return string

     */

    public function getNdsLabel()

    {

        $labels = static::getNdsLabels();


        return isset($labels[$this->with_nds]) ? $labels[$this->with_nds] : 'Неизвестный тип';

    }



View:




echo $model->ndsLabel;