Php 从函数生成IF条件

Php 从函数生成IF条件,php,if-statement,Php,If Statement,我有一个if语句,在两个不同的函数中,在相同的条件下使用。问题是,这些条件最终相当长(5-7个OR),将来可能需要修改。如果它们最终被修改,两个函数中的条件将以相同的方式更改 一个if语句的示例: if ($this->object === 'one' || $this->object === 'two' || $this->object === 'three' ) { echo 'Yes!'; } else { echo 'No!'; }; 我正在考虑创建一

我有一个if语句,在两个不同的函数中,在相同的条件下使用。问题是,这些条件最终相当长(5-7个OR),将来可能需要修改。如果它们最终被修改,两个函数中的条件将以相同的方式更改

一个if语句的示例:

if ($this->object === 'one' || $this->object === 'two' || $this->object === 'three' ) {
    echo 'Yes!';
} else {
    echo 'No!';
};
我正在考虑创建一个数组和一个函数,该数组和函数将传入if语句,但我想不出任何方法让if语句检查条件,而不是检查输入的存在

$this->object = 'one';
$test_array = array('one', 'two', 'three');

function stmbuilder($array) {
    $count = count($array);
    $stm = '';

    for ($i = 0; $i < $count; $i++) {
        $intro = '$this->object ===';
        $connector = ($i < $count-1 ? ' || ' : '');
        $stm .= $intro . $array[$i] . $connector;
    }

    return $stm;
}

$condition = stmbuilder($test_array);

if ($condition) {
    echo 'Yes!';
} else {
    echo 'No!';
} //Will always echo Yes! since $condition has a value but does not check against what $this->object is
$this->object='one';
$test_array=数组('1','2','3');
函数stmbuilder($array){
$count=计数($array);
$stm='';
对于($i=0;$i<$count;$i++){
$intro='$this->object==';
$connector=($i<$count-1?'| |':“”);
$stm.=$intro.$array[$i].$connector;
}
返回$stm;
}
$condition=stmbuilder($test_数组);
如果($条件){
回声‘是的!’;
}否则{
回音‘不!’;
}//将始终响应是!因为$condition有一个值,但不检查$this->object是什么

感谢您的帮助

为什么不把
放在数组()中呢

if(在数组中($this->object,$test\u数组,true))
$arr = array('one', 'two', 'three');

in_array('four', $arr) -> false
in_array('two', $arr) -> true