如何在EvalMath类(PHP)中堆叠许多变量和函数?
对不起,我的英语不好 我正在使用EvalMath类构建一个化学函数解释器 该班的工作包括:如何在EvalMath类(PHP)中堆叠许多变量和函数?,php,math,eval,Php,Math,Eval,对不起,我的英语不好 我正在使用EvalMath类构建一个化学函数解释器 该班的工作包括: $math->evaluate("xx = 2"); $math->evaluate("yy = 2"); $math->evaluate("zz = xx + yy"); echo $math->evaluate("zz"); //print 4 但我需要这个: $math->evaluate("xx = 2"); $math->evaluate("zz = xx
$math->evaluate("xx = 2");
$math->evaluate("yy = 2");
$math->evaluate("zz = xx + yy");
echo $math->evaluate("zz"); //print 4
但我需要这个:
$math->evaluate("xx = 2");
$math->evaluate("zz = xx + yy"); //yy is undefined
$math->evaluate("yy = 2");
echo $math->evaluate("zz"); //empty
我的变量和函数在一个数据库中,有数百个,所以我不能指定函数的顺序。
因此,我需要累积变量值,以便最终只在特定时间进行计算
$c["xx"] = new literal(2);
$c["zz"] = new plus("xx", "yy", $c);
$c["yy"] = new literal(2);
class plus {
var $a;
var $b;
var $array;
function __construct($a, $b, &$c) {
$this->a = $a;
$this->b = $b;
$this->array = &$c;
}
public function evaluate() {
return
$this->array[$this->a]->evaluate()
+
$this->array[$this->b]->evaluate();
}
}
class literal {
var $a;
function __construct($a) {
$this->a = $a;
}
public function evaluate() {
return $this->a;
}
}
echo $c["zz"]->evaluate();
我认为答案一定在于在类中使用“$this->v[]”变量,但我没有足够的知识来修复它
有人能帮我吗?提前感谢。查看该类,该值似乎是在调用->evaluate时计算的 如果您真的不关心效率,请将代码放在一个循环中,并尽可能多次地调用语句,那么这个数字只适用于最坏的情况,即每次只接受最后一个语句,为了提高效率,您可以将失败的代码记录在队列中,如果失败,则重新添加到队列中,并一直持续到空为止
for ($i =0; $i <3; $i++){
$math->evaluate("xx = 2");
$math->evaluate("zz = xx + yy"); //yy is undefined
$math->evaluate("yy = 2");
}
echo $math->evaluate("zz"); //empty
伟大的谢谢你,大卫!“for”解决方案最适合我的情况。函数使用了许多复杂的运算符,所以我不能使用第二个解决方案,但它对其他情况很有用。再次感谢!