【问题标题】:Strict Comparison in PHPPHP中的严格比较
【发布时间】:2012-02-22 12:21:39
【问题描述】:

我需要在 php 中编写一个 Player 类,该类与我不允许更改的函数一起使用。我必须以该函数将返回最大值的方式编写此类。我只能使用 1-10 之间的整数。我这里只复制了有问题的部分:

function CalcPlayerPoints($Player) {

$Points = 0;

    foreach($Player as $key => $Value) {

    switch ($key) {
    case "profyears":
        if($Value===true) // this should be true
        $Points+=($Value*5); // this should take tha value I give in the class construct
        break;
    case "gentleman":
        if($Value===true) 
        $Points+=10;                
        break;
    }
   }
return $Points; // that should be maximized
}

由于我无法更改 === 比较,我无法初始化 profyears 属性。如果我用10初始化,那么就不会进入if语句...

public function __construct() {
   $this->gentleman = true;
   $this->profyears = 10;  
}

【问题讨论】:

  • 如果 $value === true,你希望解释器如何将它乘以 5?
  • CalcPlayerPoints 怎么叫?
  • @JLevett 这是有效的 php。类型转换后,true 评估为 1,false 评估为 0;所以 boolean * int 是有效的 PHP。
  • @Mob:我错过了什么吗?没有类型转换。并且 OP 的 $Value 正试图成为严格意义上的 true 和正下方的整数。我认为这行不通。
  • @Mob 这是有效的 PHP,但几乎可以肯定不是 OP 真正想要做的。最终结果将是$Points += 5,如果这就是他实际在做的事情,他为什么不直接写呢?

标签: php comparison integer boolean strict


【解决方案1】:

此函数允许的唯一选项是 profyears 是一个布尔值,即真或假。没有其他选择。

因此,该课程不是将 profyears 视为年数,而是将其视为是否有 profyears。所以你 __construct 中唯一正确的值是真或假。这可能是一个奇怪的命名转换。如果将其命名为:例如 hasProfYears 将是有意义的。

一些例子:
有专业的绅士给:15分。
有学业的非绅士给5分。
没有专业的绅士给10分。
非君子无学业得0分。

【讨论】:

    【解决方案2】:

    此功能无法按创建者的预期工作。 $Value 变量被评估为严格布尔值,但随后对其进行了数学运算。不修改原来的功能是没有办法解决这个问题的。

    另外,似乎缺少一个右括号。

    按原样调用的函数:

    function index()
    {
       var_dump( $this->CalcPlayerPoints(array( 'profyears' => 10 )) );
    }
    
    function CalcPlayerPoints($Player) {
    
      $Points = 0;
    
         foreach($Player as $key => $Value) {
    
            switch ($key) {
                case "profyears":
                    if($Value===true) // this should be true
                    $Points+=($Value*5); // this should take tha value I give in the class construct
                    break;
                case "gentleman":
                    if($Value===true) 
                    $Points+=10;                
                    break;
    
            }
         }
    return $Points; // that should be maximized
    }
    

    无论您提供什么整数值,每次都会显示int 0。如果可以修改原始函数以消除严格比较,例如:

    function index()
    {
       var_dump( $this->CalcPlayerPoints(array( 'profyears' => 10 )) );
    }
    
    function CalcPlayerPoints($Player) {
    
      $Points = 0;
    
         foreach($Player as $key => $Value) {
    
            switch ($key) {
                case "profyears":
                    if($Value==true) // this should be true
                    $Points+=($Value*5); // this should take tha value I give in the class construct
                    break;
                case "gentleman":
                    if($Value==true) 
                    $Points+=10;                
                    break;
    
            }
         }
    return $Points; // that should be maximized
    }
    

    该函数将返回预期结果:int 50

    【讨论】:

    • 认为值得一提的是,该函数需要对对象的引用(旧的 & 引用风格)而不是数组。
    • 所讨论的函数没有区别。 OP 似乎是这样使用它的,但是数组是这个函数的一个完全有效的参数,对于测试来说应该足够了。
    • 如果没有提到这一点你是对的:我只复制了这里有问题的部分 这在本主题的开头提到了。 $Player 也以大写的 P 开头,通常表示一个对象。所以它可能会影响我们在这里看不到的部分,这就是我记下它以防止以后出现问题的原因。
    • 这个测试是为了检查这一功能逻辑的有效性而严格设置的。它就是这样做的。较大项目的背景无关紧要。
    • 另外,我拒绝认为我们知道$Player 参数只是通过使用命名约定而被引用的对象。这是一个无法根据上下文进行验证的假设。
    【解决方案3】:

    CalcPlayerPoints 函数似乎有一个错误,因为这样做很有意义:

    if ($Value === true)
        $Points += $Value * 5;
    

    即,“TRUE 乘以 5”没有任何意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-03
      • 2013-07-20
      • 2017-09-21
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      相关资源
      最近更新 更多