The following small series for everyone to bring a cliché PHP object-oriented interpreter mode. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Recently looking at "in-depth PHP object-oriented model and practice", learning the contents of the book in a moment feel a bit tall on the, ha! Actually, it's a dish B. I believe there will be a novice friend in this (I am also a novice), the book I personally think the more difficult content of the learning experience would like to come out to share and exchange, 1 is the hope of their knowledge can play to consolidate and deepen the role of understanding 2 is hoping to see this article and interested in the novice friends some help.
This part of the content to read several times the code has also been knocked several times, it is estimated that this article would like to implement the function is probably the user on the Web page to enter some content, and then through the background program parsing after the reply (feeling is in the nonsense). For example, I type in the Foreground Web page input box:
$input = "4"; $input equals "4" or $input equals "four";
Then submit, the system will be similar to "conditional" or "conditional" results (a bit similar directly in the foreground to write code and run, background parsing will return a result.) Although the original book does not explain the entire foreground input to the background parsing process but I guess this background parsing should also have a use of regular expressions to extract similar to the above 2 lines of code in the process of the keyword)
The above two lines of code, although the author invented the language, but according to the literal meaning is not difficult to understand, the first line is to define a variable and assign a value, the second row is a variable to make a judgment (the variable equals 4 or four).
Not much nonsense. Let's take a look at the classes defined by this pattern (see the original for the class diagram yourself):
Interpretercontext This class is like a container that is primarily used to store and retrieve the results of values and comparisons that need to be compared, such as 4 in the above code, four, and a comparison result of "true" or "false", The saved form is an array-like property of the class $expressionstore, with the following code:
Class interpretercontext{ Private $expressionstore = Array ();//Store The value of the comparison and result function replace (Expression $exp, $ Value) {//Set values $this->expressionstore[$exp->getkey ()] = $value; } Function lookup (Expression $exp) {//Get value return $this->expressionstore[$exp->getkey ()];} }
This class is like a tool for other classes to use (it has no inheritance, composition, or aggregation relationships with other classes).
Second, expression this is an abstract class of expressions that defines the abstract method interpret () and method Getkey ()
The code is as follows:
Abstract class Expression { private static $keycount = 0; Private $key used for counting ; Store a unique value //main implementation of the data obtained from the foreground into the above Interpretercontext class, see the following content will be found to inherit his class called the Interpretercontext class's replace () method abstract function interpret (Interpretercontext $context); Gets a unique value for function GetKey () { if (!isset ($this->key)) {self :: $keycount + +; $this->key= self:: $keycount; } return $this->key; }}
The class that will be described below inherits this class, and he and operatorexpression (the operator expression abstract class) are a combination of relationships, That is, operatorexpression can include all subclasses that inherit expression when initialized (this is also the book has been emphasized to interface programming, this expression is an interface, this interface can be used to achieve polymorphism, I don't know if I pretend B is right, ha! You can see the class diagram of the original book in detail.
literalexpression literal expression class, the function is to save a string to Interpretercontext in this small container, save as an indexed array, such as saving the beginning of the two sentences in the self-created code of 4 or four
The code is as follows:
Class Literalexpression extends expression{ private $value; function __construct ($value) {//Initialize the value passed in to be saved $this->value= $value; } function interpret (Interpretercontext $context) {//Call replace () of Interpretercontext class will be $ Value is saved to Interpretercontext in this small container $context->replace ($this, $this->value); }}
Four, the variableexpression variable expression class, and the function of the above class is the same, but the data will be saved as an associative array, the correlation array of the health is the variable name, the value is the value of the variable, such as the first two sentences in the variable "input" and the value "4",
The code is as follows:
Class VariableExpression extends expression{ private $name; Variable name private $val; Variable Value function __construct ($name, $val =null) { $this->name = $name; $this->val = $val; } function interpret (Interpretercontext $context) { if (!is_null ($this->val)) { $context->replace ($this , $this->val); $this->val = null; } } function SetValue ($value) {//used to set the value of the variable $this->val = $value; } function GetKey () {//This copies the GetKey () method of the parent class, and when the lookup () method of the small container Interpretercontext calls the GetKey () method of the instance of the class, it returns a string (that is, the variable name) Instead of the numeric index return $this->name; }}
Operatorexpression operator Expression Abstract base class, this class inherits and combines the expression abstract base class, the implementation of the interpret () method mainly saves the expression evaluation results
The code is as follows:
Abstract class Operatorexpression extends expression{protected $l _op; The value to the left of the expression protected $r _op; The value to the right of the expression function __construct (expression $l _op,expression $r _op) {//initialization can be combined to inherit the subclass instance of the expression class $this->l_op = $l _op ; $this->r_op = $r _op;} function interpret (Interpretercontext $context) { //is primarily used to save the results of the expression test (saved to an instance of the Interpretercontext Class) $this->l_op- >interpret ($context); Saves the value or calculation result of an expression subclass instance to an instance of the Interpretercontext class $this->r_op->interpret ($context); $result _l = $context- >lookup ($this->l_op); Gets the value of the previous step or results $result_r = $context->lookup ($this->r_op); $this->dointerpret ($context, $result _l, $result _r ); The specific comparison operation is implemented by inheriting subclasses to implement}protected abstract function Dointerpret (Interpretercontext $context, $result _l, $result _r);}
Six, Equalsexpression, Booleanorexpression, Booleanandexpression, respectively, are inherited the Operatorexpression abstract base class equality expression, or expression, There is only one method with the expression Dointerpret () internally calls the replace () method of the Interpretercontext class to save the evaluation of the expression to an instance of the Interpretercontext class
The code is as follows:
An equality expression class Equalsexpression extends Operatorexpression {protected function Dointerpret (Interpretercontext $context, $result _l, $result _r) {$context->replace ($this, $result _l = = $result _r);}} or expression class Booleanorexpression extends Operatorexpression{protected function Dointerpret (Interpretercontext $ Context, $result _l, $result _r) {$context->replace ($this, $result _l | | $result _r);}} With an expression class Booleanandexpression extends Operatorexpression{protected function Dointerpret (Interpretercontext $ Context, $result _l, $result _r) {$context->replace ($this, $result _l && $result _r);}}
So far this mode-related class is complete, the above code is tested, you can directly copy and paste run to see the results, now we look at the client code:
Client code One:
$context = new Interpretercontext (); $statement = new Booleanorexpression (//You can try to change this operator expression to booleanandexpression run it and see the execution Results//You can try to change the parameters instantiated in literalexpression to other values to see the results of the operation, or change the Equalsexpression object directly to Booleanorexpression or booleanandexpression New Equalsexpression (' New Literalexpression '), New Literalexpression (' Four '), New Equalsexpression (new Literalexpression (' B '), New Literalexpression (' 4 ')); $statement->interpret ($context); if ($context->lookup ($ Statement) {echo ' condition is set ';} else {echo ' condition is not established ';}
client code two :
$context = new Interpretercontext (); $statement = new Booleanorexpression (new Booleanandexpression (New equalsexpression (New Literalexpression (' 4 '), New Literalexpression (' 4 ')), new Equalsexpression (New Literalexpression (' 4 '), new Literalexpression (' 4 ')), new Equalsexpression (New Literalexpression (' B '), New Literalexpression (' 4 ')); $statement- >interpret ($context), if ($context->lookup ($statement)) {echo ' condition is set ';} else {echo ' condition is not established ';}
Client code Three:
This is the difference between the client code instance of the original and the above client code is the use of the variable expression variableexpression
$context = new Interpretercontext (); $input = new VariableExpression (' input '); This defines a variable input but is not assigned a value $statement = new Booleanorexpression (new Equalsexpression ($input, New Literalexpression (' four ') ),//Here the value of the variable expression and literal expression will be compared to a comparison of whether the new Equalsexpression ($input, New Literalexpression (' 4 '))), a foreach (Array ("Four", "4" , "->setvalue") as $val) {$input ($val); The value of the variable input to input is: $val:<br/> "; $statement->interpret ($context); Make a comparison and deposit the results of the comparison into the Interpretercontext object instance if ($context->lookup ($statement)) {//Get the results of the comparison print "condition <br/>";} else { Print "Condition not established <br/>";}}
The above code can be tested to run normally, a friend needs to copy down, run a look at the results.