Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ZendFramework:如何降低php代码的脆弱性?_Php_Design Patterns_Zend Framework2 - Fatal编程技术网

ZendFramework:如何降低php代码的脆弱性?

ZendFramework:如何降低php代码的脆弱性?,php,design-patterns,zend-framework2,Php,Design Patterns,Zend Framework2,刚接触Zend Framework 2的实习生,一直在尝试使此代码更具适应性: class UserCohort { private $toString; function __construct($count) { $this->count = $count; if ($count < 5000) { $this->toString = "less_than_5k"; } els

刚接触Zend Framework 2的实习生,一直在尝试使此代码更具适应性:

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;
    }
}
类用户队列{
私人$toString;
函数构造($count)
{
$this->count=$count;
如果($count<5000){
$this->toString=“小于5k”;
}否则,如果($count<25000){
$this->toString=“5k到25k”;
}否则,如果($count<100000){
$this->toString=“2.5万到10万”;
}否则{
$this->toString=“大于10万”;
}
}
公共函数
{
返回$this->toString;
}
}
试图通过范围支持获得类似枚举的功能,即usercourt(4998)=usercourt(4999)等,具体取决于您的编程方式。我知道如何在Java中实现这一点,但是有没有比上面的php解决方案更好的方法呢

并没有发现Spleum类是有帮助的,想不出任何其他的设计模式来降低代码的脆弱性,想到了factory,但对于这样简单的功能来说,这似乎是一个很大的工作


谢谢

如果像这样比较对象实例,它将与您预期的不一样。因为这意味着要比较
对象
而不是字符串结果。 如果要比较对象生成的字符串,需要先将
对象
转换为字符串。或者你可以这样做

strval(UserCohort(4998)) == strval(UserCohort(4999));

您可以对字符串使用一些常量,并使用开关而不是if-else子句:

class UserCohort {
    const LESS_THEN_5K = "less_than_5k";
    const RANGE_5K_TO_25K = "5k_to_25k";
    const RANGE_25K_TO_100K = "25k_to_100k";
    const GREATER_THEN_100K = "greater_than_100k";

    private $string;

    private $count;

    function __construct($count)
    {
        $this->count = $count;
        switch(true){
            case $count < 5000:
                $this->string = self::LESS_THEN_5K;
                break;
            case $count < 25000:
                $this->string = self::RANGE_5K_TO_25K;
                break;
            case $count < 100000:
                $this->string = self::RANGE_25K_TO_100K;
                break;
            default:
                $this->string = self::GREATER_THEN_100K;
        }
    }

    public function __toString()
    {
        return $this->string;
    }
}
类用户队列{
const LESS_THEN_5K=“LESS_than_5K”;
常数范围为5K到25K=“5K到25K”;
const RANGE_25K_至_100K=“25K_至_100K”;
const GREATER_THEN_100K=“GREATER_than_100K”;
私人$string;
私人$count;
函数构造($count)
{
$this->count=$count;
开关(真){
案例$count<5000:
$this->string=self::LESS_THEN_5K;
打破
案例$count<25000:
$this->string=self::范围从5K到25K;
打破
案例$count<100000:
$this->string=self::范围从25K到100K;
打破
违约:
$this->string=self::大于100K;
}
}
公共函数
{
返回$this->string;
}
}
但我想这更多的是关于美学,而不是一个合适的选择。 也许你还应该考虑检查变量类型<代码> $Cube <代码>,作为输入,如果不是数字,则抛出异常。< /P>