Classes and variables from the URL in PHP
Object oriented programming in PHP is a powerful possibility that is briefly explored and utilized in this example. Variables are passed in the URL and the program creates objects and calls functions based on the values of these variables, using classes and functions.
example.php contains all the PHP code. Two variables are used:
- country
- likeit
If the variable country equals england, then an object stored in the variable england will be created from the England class. The value of the variable likeit will be evaluated from the England class, and the three possible values for this variable are:
- yes
- no
- Any value different from yes and no, including NULL if the value is not set.
The variable likeit will be evaluated from the classes, either England or USA class. So if the variable country is different from england and usa, it does not matter the value of the variable likeit because it will not be evaluated, and the message "The country is not England nor USA." will be displayed.
The inputs/outputs are represented in the following table:
| country/likeit | yes | no | other |
|---|---|---|---|
| england | Print "We are in England and you like it." | Print "We are in England and you do not like it." | Print "We are in England and you are not sure if you like it." |
| usa | Print "We are in USA and you like it." | Print "We are in USA and you do not like it." | Print "We are in USA and you are not sure if you like it." |
| other | Print "The country is not England nor USA." | Print "The country is not England nor USA." | Print "The country is not England nor USA." |
A decision table is an alternative way to test input/outputs for this program:
| Rules | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Conditions | country=england | T | T | T | F | F | F | F | F | F |
| country=usa | F | F | F | T | T | T | F | F | F | |
| country!=england && country!=usa | F | F | F | F | F | F | T | T | T | |
| likeit=yes | T | F | F | T | F | F | T | F | F | |
| likeit=no | F | T | F | F | T | F | F | T | F | |
| likeit!=yes && likeit!=no | F | F | T | F | F | T | F | F | T | |
| Actions | Print "We are in England and you like it." | X | ||||||||
| Print "We are in England and you do not like it." | X | |||||||||
| Print "We are in England and you are not sure if you like it." | X | |||||||||
| Print "We are in USA and you like it." | X | |||||||||
| Print "We are in USA and you do not like it." | X | |||||||||
| Print "We are in USA and you are not sure if you like it." | X | |||||||||
| Print "The country is not England nor USA" | X | X | X | |||||||
For any input (inputs are the values for the variables assigned from the URL), one of nine outputs are possible:
Input/Output one
Input: example.php?country=england&likeit=yes
Output: "We are in England and you like it."
Input/Output two
Input: example.php?country=england&likeit=no
Output: "We are in England and you do not like it."
Input/Output three
Input: example.php?country=england
Output: "We are in England and you are not sure if you like it.
Input/Output four
Input: example.php?country=usa&likeit=yes
Output: "We are in USA and you like it."
Input/Output five
Input: example.php?country=usa&likeit=no
Output: "We are in USA and you do not like it."
Input/Output six
Input: example.php?country=usa
Output: "We are in USA and you are not sure if you like it."
Input/Output seven
Input: example.php?country=x&likeit=yes
Output: "The country is not England nor USA"
Input/Output eight
Input: example.php?country=x&likeit=no
Output: "The country is not England nor USA"
Input/Output nine
Input: example.php
Output: "The country is not England nor USA"
The PHP file example.php contains the following code: