PHP:如何能将我的变量一组动态的条件? [英] PHP: How can I apply my variables to a dynamic set of conditions?

查看:166
本文介绍了PHP:如何能将我的变量一组动态的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑数组作为组通用或抽象的条件语句:

$ conditionSet =阵列(
    条件1=> '$ A> $ B',
    '条件2'=> '$ C!= $ D',
    condition3'=> '$ F< $ E'
);

是否有可能在我当前执行的适用范围,我有变数的条件上即时阵列中 - 无需尝试解析的语句?

$ a = 1;
$ B = 2;
$ C = 3;
$ D = 4;
$ E = 5;
$ F = 6;如果($ conditionSet ['条件1']和放大器;&安培;
     $ conditionSet ['条件2']&放大器;&安培;
     $ conditionSet ['condition3'])
{
    // PASS:做点什么
}其他{
    //失败:做别的事情
}


解决方案

我想我看到你做了什么 - 其他的答案这里假设你的 $ conditionSet 是predetermined布尔值的集合,这是完全合理的,但您的编辑建议您希望能够断言 N 的参数数目匹配给定一般检查在任何给定的时间?

而不是耦合的状态以您的应用程序的特定部分,你能集团 调用 比较的功能的(断言)的。虽然它可能不符合严格的它在这个例子中,我相信这个形式 - 部分 - 一个函数式编程的基础的方法。

函数断言(){
    的$ args = func_get_args();
    的foreach(的$ args [0]为$断言)
        如果(!call_user_func_array($断言,array_slice($ ARGS,1)))
            返回false;
    返回true;
}$ conditionSet = [
    功能($ A,$ B){返回$ A> $ B; },
    功能($ A,$ B,$ C,$ D){$收益C = $ d!个; },
    功能($ A,$ B,$ C,$ D,$ E,$ F){$回报F< $ê; }
];如果(断言($ conditionSet,2,1,3,4,6,5))
    回声所有条件,真正的;
其他
    呼应一个或多个条件假;

演示

变量 $ A $ F 可作为一个单一的阵列可以通过,但我想证明的灵活性。


您当然可以只使用评估但坦率地说,这将是的fugly的(大写F)的!这种方法更强大,可以说是接近你的问题的正确方法。我也觉得这是相当pretty但这是主观的:) - 我不知道它将如何影响性能。

您应该检查在 call_user_func call_user_func_array ,因为他们是非常强大的工具。


下面是只是为了完整性,不应在生产code使用...

我应该也指出的 PHP 的有它自己的断言函数,它接受调用参数的(当然我也不自己意识到这一点)的,但它不接受的一组数。在任何情况下,这里有一个简单的例子:

$ a = 2; $ B = 3;
如果(断言(函数($ A,$ B){$ A> $ B;}))
    回声$ a是大于$ b';

如果你最终不会阅读 断言 文档请注意有读取部分:


  

......应作为一个调试功能只是......不应该被用于类似的输入参数检查正常运行时操作。作为一个经验法则你的code应该总是能工作正确如果断言检查没有被激活。


我不知道这是否是指适用于功能,在它的全部或只是它的评估字符串的(最有可能是前者)的能力的一个选项 - 我猜作品以类似的方式,以评估

查看这个帖子为什么评估是邪恶

这不是 100%的明确的最终意图是什么,对于简单的比较,这是有点矫枉过正,但我​​可以看到我发布的,如果你正在处理更复杂的逻辑第一溶液中使用。

Consider an array as a set of generic or abstract conditional statements:

$conditionSet = array(
    'condition1' => '$a > $b', 
    'condition2' => '$c != $d', 
    'condition3' => '$f < $e'
);

Is it possible to apply the variables that I have in my current scope of execution to the conditions in the array on-the-fly - without having to try and parse the statements? i.e.

$a = 1; 
$b = 2; 
$c = 3; 
$d = 4;
$e = 5; 
$f = 6; 

if ( $conditionSet['condition1'] && 
     $conditionSet['condition2'] && 
     $conditionSet['condition3'] )
{
    // PASS: do something    
} else {        
    // FAIL: do something else
}

解决方案

I think I see what you are getting at - the other answers here assumed that your $conditionSet is a collection of predetermined booleans which is perfectly plausible but your edit suggests that you want to be able to assert that n number of parameters match a given set of generic checks at any given time?

Instead of coupling state to particular parts of your application you could group callable "comparison" functions (assertions). Although It may not adhere to it strictly in this example I believe this forms - in part - the basis of a functional programming approach.

function assertion(){
    $args = func_get_args();
    foreach($args[0] as $assert)
        if(!call_user_func_array($assert, array_slice($args, 1)))
            return false;
    return true;
}

$conditionSet = [
    function($a, $b){ return $a > $b; },
    function($a, $b, $c, $d){ return $c != $d; },
    function($a, $b, $c, $d, $e, $f){ return $f < $e; }
];

if( assertion($conditionSet, 2, 1, 3, 4, 6, 5) )
    echo 'All conditions true';
else 
    echo 'One or more conditions false';

demo

Variables $a through $f could be passed as a single array but I wanted to demonstrate the flexibility.


You could of course just use eval but frankly that would be Fugly (capital F)! This method is more robust and arguably the proper way to approach your problem. I also think it's rather pretty but that's subjective :) - I'm not sure how it would affect performance.

You should check out the documentation on call_user_func and call_user_func_array as they are quite powerful tools.


The following is just for completeness and should not be used in production code...

I should have also pointed out that PHP has it's own assert function which accepts callable arguments (admittedly I wasn't even aware of this myself) but it doesn't accept a set of several. In any case here's a simple demonstration:

$a = 2; $b = 3;
if( assert(function($a, $b){ $a > $b; }) )
    echo '$a is greater than $b';

If you don't end up reading the assert documentation please be aware there's a part that reads:

"... should be used as a debugging feature only ... should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated."

I'm not sure if this refers to an option that applies to the the function in it's entirety or just it's ability to evaluate strings (most likely the former) - which I'm guessing works in a similar way to eval.

See this post on why eval is evil.

It's not 100% clear what your final intention would be, for simplistic comparison this is somewhat overkill but I can see a use for the first solution I posted if you are dealing with more complex logic.

这篇关于PHP:如何能将我的变量一组动态的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆