Stilistic Dev /// Munky Style head_bg head end
nav begin hometutorialsgallerydownloadsservicesportfoliocontactabout
default   blue   orange   black   purple   yellow   teal   green   red  


Google
 
Web StilisticDev.net
mhtr Intro To Object: Creating Your First Classmhtr
 
mbtl   mbtr
  You have havent done so yet, I strongly recommend you first read my Intro To Object before starting this tutorial.

First thing we need to do is create two files. One that will contain our class, name this one func_math.php. The next file will be used to call the class and function, you can name this whatever you like.

First we have the func_math.php


<?
class math {

function add($num1, $num2){
$sum = $num1 + $num2;
$result = "$num1 + $num2 = $sum";
return
$result;
}
}
?>


next we have math.php (like I said, you can name this whatever you like)


<?

require_once 'func_math.php';

$obj_math = new math();

$sum = $obj_math->add("2", "4");
echo
$sum;

?>




Now for the break down. First we have


class math {


All this does I creates the name of the class

next we have:


function add($num1, $num2){


This is where we start our function. Notice the ($num1, $num2). This is where we set the variables to use in our function. When we call the function, we pass values that will be given to these variables. Things will get much clearer when we call the function.

Now for what the function does.


$sum = $num1 + $num2;
$result = "$num1 + $num2 = $sum";
return
$result;
}
}


This will create a sum of the two values passed to the function. First we have
$sum = $num1 + $num2. The $num1 and $num2 are the same variables we set in the function add().
Next we set $result to create the code that will display the calculation
then we retun result. Retun tells the function to return this value when we call the functon. The return has to be the last part of the function, because after return, the function stops, kind of like when you use exit. any code after that will not be seen. You can return more than one value, but you cant do it by saying:
return $val1
return $val2
because remember, the code stops after the first return. if you want to return more than one value, you have to do an if statement. pretty much if this then return $val1, else return $val2. I wont go into that right now, because its more advanced, its something that will be in a different tutorial. Just know, you can only return once, unless you modify the code to return different values depending on different situations.

Thats it for the class, so now let use it. the fun part :)

first we need to include the file our class is in. which is done by:


require_once 'func_math.php';


Next we need to instanciate the class. remember, do this AFTER you include the class file


$obj_math = new math();


the $obj_math is the variable we give the class, this will be used when we call the function. the new math is new name of class (which is math).

Now the good stuff, calling the function.


$sum = $obj_math->add("2", "4");
echo
$sum;



first we set a variable for our function, I use $sum. Then we say = $obj_math->add. The $obj_math is the variable we used when we instanciated the class. the add is the name of the function we are calling. Now we have("2", "4");
these are the 2 values we are sending to the function. Rembmber the ($num1, $num2)? these values will replace those.
so when we set these values, and they are passed to the class, the class would do:


$sum = 2 + 4;


Next we echo out $sum, which will return the variable we said to in the class (which was the $result). so when we echo $sum we will get:

2 + 4 = 6

now say we call the function like this:


$sum = $obj_math->add("250", "30");
echo
$sum;



when we echo $sum well get:

250 + 30 = 280. pretty nifty huh?

Congradulations, you just wrote your first class. Now you could add some more function to the class, such as ones to subtract, multiply, and divide. then you'd have a class that acts as a basic calculator.






 
mbbl   mbbr