Classes and variables from the URL in PHP


Posted on April 9, 2010 by Jaime Montoya

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:

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:

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:

<html>
<head>
<title>Classes and variables from the URL in PHP</title>
</head>
<body>
<ul>
<li>England</li>
<ul>
<li><a href="example.php?country=england&likeit=yes">I like it</a></li>
<li><a href="example.php?country=england&likeit=no">I do not like it</a></li>
<li><a href="example.php?country=england">I am not sure if I like it</a></li>
</ul>
<li>USA</li>
<ul>
<li><a href="example.php?country=usa&likeit=yes">I like it</a></li>
<li><a href="example.php?country=usa&likeit=no">I do not like it</a></li>
<li><a href="example.php?country=usa">I am not sure if I like it</a></li>
</ul>
</ul>
<h1>Result:</h1>
<?php
class England{
function proxy($likeit){
switch ($likeit){
case 'yes':
echo "We are in England and you like it.";
break;
case 'no':
echo "We are in England and you do not like it.";
break;
default:
echo "We are in England and you are not sure if you like it.";
break;
}
}
}
class USA{
function proxy($likeit){
switch ($likeit){
case 'yes':
echo "We are in USA and you like it.";
break;
case 'no':
echo "We are in USA and you do not like it.";
break;
default:
echo "We are in USA and you are not sure if you like it.";
break;
}
}
}
switch($_GET['country']){
case 'england':
$england = new England;
$england->proxy($_GET[likeit]);
break;
case 'usa':
$usa = new USA;
$usa->proxy($_GET[likeit]);
break;
default:
echo "The country is not England nor USA.";
break;
}
?>
</body>
</html>
HomeDesignProgrammingArchivesContactAbout