• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP解释器模式的一个简单示例分享

php 搞代码 3年前 (2022-01-22) 22次浏览 已收录 0个评论

PHP解释器模式的一个简单示例分享

<?php// 解释器模式abstract class Expression{	private static $keyCount = 0;	private $key = NULL;	abstract function interpret(InterpreterContext $ctx);	/**	 * as array key	 * @return auto increment value	 */	public function getKey()	{		if(!isset($this->key)) {			self::$keyCount++;			$this->key = self::$keyCount;		}		return $this->key;	}}/** * context */class InterpreterContext{	private $expressionstore = array();	/**	 * store value	 */	public function replace(Expression $exp, $value)	{		$this->expressionstore[$exp->getKey()] = $value;	}	/**	 * find value	 */	public function lookup(Expression $exp)	{		return $this->expressionstore[$exp->getKey()];	}}/** * literal expression */class LiteralExpression extends Expression{	private $value;	public function construct($value)	{		$this->value = $value;	}	public function interpret(InterpreterContext $ctx)	{		$ctx->replace($this, $this->value);	}}/** * var=value expression */class VariableExpression extends Expression{	private $name;	private $val;	public function construct($name, $val=null)	{		$this->name = $name;		$this->val = $val;	}	public function interpret(InterpreterContext $ctx)	{		if(!is_null($this->val)) {			$ctx->replace($this, $this->val);			$this->val = null;		}	}	public function setValue($value)	{		$this->val = $value;	}	/**	 * @override	 */	public function getKey()	{		return $this->name;	}}abstract class OperatorExpression extends Expression{	protected $l_op;	protected $r_op;	public function construct(Expression $l, Expression $r)	{		$this->l_op = $l;		$this->r_op = $r;	}	/**	 * @param $ctx 		 InterpreterContext	 * @param $result_l  Expression $l's result	 * @param $result_r  Expression $r's result	 */	protected abstract function doInterpret(InterpreterContext $ctx, 		$result_l, $result_r);	public function interpret(InterpreterContext $ctx)	{		$this->l_op->interpret($ctx);		$this->r_op->interpret($ctx);		$result_l = $ctx->lookup($this->l_op);		$result_r = $ctx->lookup($this->r_op);		$this->doInterpret($ctx, $result_l, $result_r);	}}/** * equals */class EqualsExpression extends OperatorExpression{	protected function doInterpret(InterpreterContext $ctx, $result_l, $result_r)	{		$ctx->replace($this, $result_l == $result_r);	}}/** * or */class BooleanOrExpression extends OperatorExpression{	protected function doInterpret(InterpreterContext $ctx,<i>·本2文来源gaodai$ma#com搞$代*码网2</i><strong>搞gaodaima代码</strong> $result_l, $result_r)	{		$ctx->replace($this, $result_l || $result_r);	}}/** * and */class BooleanAndExpression extends OperatorExpression{	protected function doInterpret(InterpreterContext $ctx, $result_l, $result_r)	{		$ctx->replace($this, $result_l && $result_r);	}}/** * not */class BooleanNotExpression extends Expression{	private $expr;		public function construct(Expression $e) {		$this->expr = $e;	}		public function interpret(InterpreterContext $ctx) {		$this->expr->interpret($ctx);		$ctx->replace($this, !$ctx->lookup($this->expr));	}}// test code/*$ctx = new InterpreterContext();$literarl = new LiteralExpression('four');$literarl->interpret($ctx);// echo $ctx->lookup($literarl);$variable = new VariableExpression('input', '444');$variable->interpret($ctx);// echo $ctx->lookup($variable);$answer = new VariableExpression('input');$answer->interpret($ctx);echo $ctx->lookup($answer);*/// $input equas four or $input equals 4$ctx = new InterpreterContext();$input = new VariableExpression('input');$statement = new BooleanOrExpression(	new EqualsExpression($input, new LiteralExpression('four')),	new EqualsExpression($input, new LiteralExpression(4)));$input->setValue('four');$statement->interpret($ctx);var_dump($ctx->lookup($statement)); // true$input->setValue(4);$statement->interpret($ctx);var_dump($ctx->lookup($statement)); // true$input->setValue(42);$statement->interpret($ctx);var_dump($ctx->lookup($statement)); // false$not = new BooleanNotExpression($statement); // true$not->interpret($ctx);var_dump( $ctx->lookup($not) );

以上就是PHP解释器模式的一个简单示例分享的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP解释器模式的一个简单示例分享
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址