View Single Post
Old 03-27-2008, 03:36 PM   #1 (permalink)
quantumkangaroo
The Contributor
 
quantumkangaroo's Avatar
 
Join Date: Feb 2008
Location: Pretoria, South Africa
Posts: 42
Thanks: 1
quantumkangaroo is an unknown quantity at this point
Default Writing your first class

A mate of mine asked me today how do you write a class so i decided after giving him an explanation to make a tutorial about it. Ok so your used to the php language now right? So whats the hype about classes and functions. Lets say you are writing a large application, wouldn't it be nice if you could use the same code over and over and over and over again? It is possible, functions and classes make our lives as programmers so much easier. For this tutorial we are going to write a basic calculator, basic meaning it will only have 2 input fields and a button.

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
</body>

<?php

class Calculation {
  var 
$num1 "";
  var 
$num2 "";

  function 
Add($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; }

  function 
Subtract($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; }

  function 
Multiply($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; }

  function 
Divide($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; }
}

if(isset(
$_POST['submit'])) {

  
$num1 $_POST['Num1'];
  
$num2 $_POST['Num2'];

  
$add Calculation::Add ($num1,$num2);
  
$subtract Calculation::Subtract ($num1,$num2);
  
$multiply Calculation::Multiply ($num1,$num2);
  
$divide Calculation::Divide ($num1,$num2);

  echo 
$num1.' + '.$num2.' = '.$add.' <br>';
  echo 
$num1.' - '.$num2.' = '.$subtract.' <br>';
  echo 
$num1.' * '.$num2.' = '.$multiply.' <br>';
  echo 
$num1.' / '.$num2.' = '.$divide.' <br>';

}

?>

<form action="test.php" method="post" >
<input type="text" name="Num1" /><br/>
<input type="text" name="Num2" /><br/>
<input type="submit" name="submit" /></form>
</body>
</head>
Now lets break up the code, here is the page information.

PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<
title>Untitled Document</title>
</
head>
</
body
Now for the juicy part here our class starts

PHP Code:
<?php

class Calculation {
  var 
$num1 "";
  var 
$num2 "";
We defined our class name and cleared all variables that we will be using. We use var to declare our variables, from PHP 5 visibility keywords such as protect, private, public are available, var is still though compatible with PHP 5. Still with me? Good now lets add some functionality.

PHP Code:
  function Add($num1,$num2) {
     
$answer += $num1 $num2;

     return 
$answer; } 
Our first function is Add, which adds the to variables to we supply as an argument. It takes the 2 variables supplied in the argument adds them together then returns the $answer variable. Argument is the values, or method we supply to the function to use, we can either use default values or let pass values to the function via reference. Easy aint it? Here are the other 3 functions.

PHP Code:
  function Subtract($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; }

  function 
Multiply($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; }

  function 
Divide($num1,$num2) {
     
$answer $num1 $num2;

     return 
$answer; } 
In each of these functions we just change the arithmetic operators to produce the answer we are looking for. After the functions we close off our class.

PHP Code:

Now its time to get some values!

PHP Code:
if(isset($_POST['submit'])) {

  
$num1 $_POST['Num1'];
  
$num2 $_POST['Num2'];

  
$add Calculation::Add ($num1,$num2);
  
$subtract Calculation::Subtract ($num1,$num2);
  
$multiply Calculation::Multiply ($num1,$num2);
  
$divide Calculation::Divide ($num1,$num2);

  echo 
$num1.' + '.$num2.' = '.$add.' <br>';
  echo 
$num1.' - '.$num2.' = '.$subtract.' <br>';
  echo 
$num1.' * '.$num2.' = '.$multiply.' <br>';
  echo 
$num1.' / '.$num2.' = '.$divide.' <br>';

}

?> 
Here the real fun begins, we define our values by getting them via the POST method from our html form.

The Scope Resolution Operator or double colon :: allows us access to methods of a class. It is used in object-oriented programming when you want to be specific about what kind of function you are calling. The most common use for scope resolution is with the pseudo-class parent. For example, if you want a child object to call its parent's __construct() function, you would use parent::__construct().

Class::Function($argument1, $argument2);

Now we can echo the answers to the user. Last but not least our html form. Here we show our input boxes and submit button. Remember there is a difference between using the POST and GET methods on a form, the GET method sends the values using the http header.

PHP Code:
<form action="test.php" method="post" >
<
input type="text" name="Num1" /><br/>
<
input type="text" name="Num2" /><br/>
<
input type="submit" name="submit" /></form>
</
body>
</
head
__________________
virtueCart v1.0.5 developed by WebDevSA


Last edited by quantumkangaroo : 03-27-2008 at 04:09 PM.
Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
The Following User Says Thank You to quantumkangaroo For This Useful Post:
Gurnk (03-27-2008)