TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Writing your first class (http://www.talkphp.com/absolute-beginners/2530-writing-your-first-class.html)

quantumkangaroo 03-27-2008 03:36 PM

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


Nor 03-27-2008 03:43 PM

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.

quantumkangaroo 03-27-2008 04:08 PM

This was just a quick tutorial, ive added some detail. Enjoy

ETbyrne 03-27-2008 04:46 PM

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. :-)

quantumkangaroo 03-27-2008 04:53 PM

Yes that is another way of doing it

Ultimatum 03-27-2008 06:25 PM

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.

quantumkangaroo 03-27-2008 07:30 PM

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 (Post 12846)
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.


Ultimatum 03-27-2008 07:53 PM

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.

quantumkangaroo 03-27-2008 10:28 PM

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.


All times are GMT. The time now is 09:43 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0