Dynamic Comparison Operators in PHP

后端 未结 11 716
挽巷
挽巷 2020-12-03 11:24

Is it possible, in any way, to pass comparison operators as variables to a function? I am looking at producing some convenience functions, for example (and I know this won\'

11条回答
  •  醉酒成梦
    2020-12-03 12:14

    How about a small class:

    class compare
    {
      function is($op1,$op2,$c)
      {
         $meth = array('===' => 'type_equal', '<' => 'less_than');
         if($method = $meth[$c]) {
            return $this->$method($op1,$op2);
         }
         return null; // or throw excp.
      }
      function type_equal($op1,$op2)
      {
          return $op1 === $op2;
      }
      function less_than($op1,$op2)
      {
          return $op1 < $op2;
      }
    }
    

提交回复
热议问题