By Sergey Skudaev
In this tutorial you will learn a little about Object Oriented Programming in PHP. I will not write about theory or history of Object Oriented Programming. You can find it in Wikipedia . I give you just one simple example that helps you to understand the concept. First, I give you a simple code example in procedural style, then the same code example with using functions and then the same example with using a class. (OOP approach).
Let us create a simple form that allows us to enter two numbers and a simple PHP code that calculate sum of the numbers, its difference, its multiplication and division.
<html> <head> </head> </body> <form method="post" action="calc_procedural_code.php"> <table> <tr><td><input type="text" name="number1" size="10"></td></tr> <tr><td><input type="text" name="number2" size="10"></td></tr> <tr><td><input type="submit" name="submit" value="Calculate"></td></tr> </table> </form> </body> </html>
Save form as calculator.html file. Now let us write PHP code that performs the calculation.
<? //calc_procedural_code.php $num1=$_POST['number1']; $num2=$_POST['number2']; $sum=$num1 + $num2; $difference=$num1 - $num2; $multi=$num1 * $num2; $div=$num1/$num2; echo "Sum=".$sum." <br>"; echo "Difference =".$difference." <br>"; echo "Multilpication=".$multi." <br>"; echo "Division=".$div."<br>"; ?>
Save file as calc_procedural_code.php. Run the calculator.html form on a web server and enter any numbers like 6 and 3. Click the calculate button. The PHP script will display output:
Sum = 9 |
Difference = 3 |
Multilpication = 18 |
Division = 2 |
Let us perform the same calculation by using functions in PHP code. Create file "calc_function.php" with the following code:
<? //Define functions for each calculation action function add($number1, $number2) { $sum=$number1 + $number2; return $sum; } function subtract($number1, $number2) { $dif=$number1 - $number2; return $dif; } function multiply($number1, $number2) { $multi=$number1 * $number2; return $multi; } function divide($number1, $number2) { $dev=$number1/$number2; return $dev; } $num1=$_POST['number1']; $num2=$_POST['number2']; //call functions: $sum=add($num1, $num2); $dif= subtract($num1, $num2); $multi= multiply($num1, $num2); $div= divide($num1, $num2); //Output the result echo "Sum=".$sum." <br>"; echo "Difference =".$dif." <br>"; echo "Multilpication=".$multi." <br>"; echo "Division=".$div."<br>"; ?>
Edit calculator.html file. Change action="cacl_procedural_code.php" to action="calc_function.php" Again, run the calculator.html form on a web server and enter any numbers like 6 and 3. Click the calculate button. The PHP script will display output:
Sum = 9 |
Difference = 3 |
Multilpication = 18 |
Division = 2 |
Now let us create PHP code with class.
<? // define class class Calculator { //class variables: var $sum; var $dif; var $multi; var $div; //Class member functions or methods: function add($number1, $number2) { $this->sum=$number1 + $number2; } function substract($number1, $number2) { $this->dif=$number1 - $number2; } function multiply($number1, $number2) { $this->multi=$number1 * $number2; } function devide($number1, $number2) { $this->div=$number1/$number2; } } $num1=$_POST['number1']; $num2=$_POST['number2']; //Create a class instance: $calc = new Calculator(); //Call class methods: $calc->add($num1,$num2); $calc->substract($num1,$num2); $calc->multiply($num1,$num2); $calc->devide($num1,$num2); //Display output: echo "Sum=".$calc->sum." <br>"; echo "Difference =".$calc->dif." <br>"; echo "Multilpication=".$calc->multi." <br>"; echo "Division=".$calc->div."<br>"; ?>
Save file as calc_class.php. Edit calculator.html form and type action="calc_class.php". Run the calculator.html form, enter numbers and click Calculate button. The calculation result will display on the next page.
We are done.