【发布时间】: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