TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-27-2008, 03:36 PM   #1 (permalink)
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)
Old 03-27-2008, 03:43 PM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Its a nice "start", but you left out some major information when it comes to creating a class.

1. You didn't explain what "var" is, and how it relates to PHP5 and how the new standard for PHP5 works.
2. You didn't explain what an argument is, if I was a beginner I would be the first to ask what exactly a argument or parameter was.
3. You forgot to explain how "::" the scope worked when it came to classes. You might of "showed" us but you still didn't get into detail with how it worked.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 03-27-2008, 04:08 PM   #3 (permalink)
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

This was just a quick tutorial, ive added some detail. Enjoy
__________________
virtueCart v1.0.5 developed by WebDevSA

Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
Old 03-27-2008, 04:46 PM   #4 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Just a note. Instead of doing this...

PHP Code:
$add Calculation::Add ($num1,$num2);
$subtract Calculation::Subtract ($num1,$num2);
$multiply Calculation::Multiply ($num1,$num2);
$divide Calculation::Divide ($num1,$num2); 
...You could do this instead if you wanted:

PHP Code:
$math = new Calculation();
$add $math->Add($num1,$num2);
$subtract $math->Subtract($num1,$num2);
$multiply $math->Multiply($num1,$num2);
$divide $math->Divide($num1,$num2); 
Just another way to do the same thing.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 03-27-2008, 04:53 PM   #5 (permalink)
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

Yes that is another way of doing it
__________________
virtueCart v1.0.5 developed by WebDevSA

Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
Old 03-27-2008, 06:25 PM   #6 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Ultimatum is on a distinguished road
Default

Nice tutorials but you forgot a lot of this. You didn't set a level for the function (public, private, protected). And if you want to call a function static you need to define it in de class, for example:
public static function Add(). You define 2 vars in your class but never uses them.
Ultimatum is offline  
Reply With Quote
Old 03-27-2008, 07:30 PM   #7 (permalink)
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

There was really no need to set a level for the function i explained what the visibility keywords do, but i did not implement public, protected or private that doesnt mean i forgot about them there is no need for beginners to use these as of yet? If you want i can write an article about visibility keywords in the advanced programming section?

Quote:
Originally Posted by quantumkangaroo View Post
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.
__________________
virtueCart v1.0.5 developed by WebDevSA

Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
Old 03-27-2008, 07:53 PM   #8 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 13
Thanks: 0
Ultimatum is on a distinguished road
Default

I would appreciate a tutorial in the advanced programming section :). About the function levels, sorry about that, I read over it I think, my fault.
Ultimatum is offline  
Reply With Quote
Old 03-27-2008, 10:28 PM   #9 (permalink)
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

No problem its just to keep it as simple as possible for beginners, not giving too much detail as they would generally become confused, as for the advanced programming one let me finish my work then see what i can put together.
__________________
virtueCart v1.0.5 developed by WebDevSA

Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:07 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design