How to parse a string of boolean logic in PHP

后端 未结 3 1732
醉酒成梦
醉酒成梦 2020-12-28 09:12

I\'m building a PHP class with a private member function that returns a string value such as:

\'true && true || false\'

to a public

3条回答
  •  一个人的身影
    2020-12-28 09:36

    Let's assume eval() is an ok/good solution in your case.

    class Foo {
      private function trustworthy() {
        return 'true && true || false';
      }
    
      public function bar() {
        return eval('return '.$this->trustworthy().';');
      }
    }
    
    $foo = new Foo;
    $r = $foo->bar();
    var_dump($r);
    

    prints bool(true)

提交回复
热议问题