lundi 12 juin 2017

ZendFramework: How to make this php code less brittle?

Intern new to Zend Framework 2, been trying to make this code more adaptable:

class UserCohort {
private $toString;

function __construct($count)
{
    $this->count = $count;
    if ($count < 5000) {
        $this->toString = "less_than_5k";
    } else  if ($count < 25000) {
        $this->toString = "5k_to_25k";
    } else if  ($count < 100000) {
        $this->toString = "25k_to_100k";
    } else
        $this->toString = "greater_than_100k";
}

public function __toString()
{
    return $this->toString;
}

}

Was trying to get enum-like functionality with range support i.e. UserCohort(4998) == UserCohort(4999) etc, depending on how you program it. I know how to do this in Java but is there a better way than my php solution above?

Didn't really find SPLENUM class to be helpful, can't think of any other design patterns to make this code less brittle, thought about factory but that seems like a lot of work for such simple functionality.

Thanks!

Aucun commentaire:

Enregistrer un commentaire