Tips Centre Tuesday, April 23, 2024

Home Home | About Us About Us | Contact Us Contact Us

vrPakistanis Technologies - Web Development, Logo Designing, Web Designing

Home > PHP

Intro To Object: Option Variables

Now that we have our first simple class (which we build in Intro To Object: Building Your First Class) we're going to modify it a bit.

We're going to use the same class and function as in the last tutorial, but these will be slightly modified. We're going to make it where we can do different code based on the variables passed to the function. Again I'll refer to my db class as an example.

you have a function to do select * from table
now if you use mysql just a slight bit, you should know this is a very generic query, what if we want:
select * from table where this = '$this' limit 5
or even
select distinct(row) from table where this = '$this' limit 5

Now you could make different functions to do each, but that's a waste of time, and code. so what we do is make our function where we can say, if this variable is set, do this code. So, enough with the explanation, let look at the code

For the files again we have func_math.php:

<?
class math {

function add($num1, $num2, $num3 = ''){
if($num3 != ''){
$sum = ($num1 + $num2) * $num3;
$result = "($num1 + $num2) x $num3 = $sum";
} else {
$sum = $num1 + $num2;
$result = "$num1 + $num2 = $sum";
}
return $result;
}
}
?>

and, what I call math.php (you can name this whatever you like)

<?

require_once 'func_math.php';

$obj_math = new math();

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

?>

Since this is almost the same code as part two, im not going to break everything apart as much, just the key areas.

First lets look at func_math.php. Notice in the function math() we have a 3rd variable, which looks like this

$num3 = ''

This is setting up an option variable (theres really no official name for this that I know of, so I just call it an option variable). What this lets you do is, basicly break your function into two parts depending on if this variable is passed or not. Which is done with this code:

if($num3 != ''){
$sum = ($num1 + $num2) * $num3;
$result = "($num1 + $num2) x $num3 = $sum";
} else {
$sum = $num1 + $num2;
$result = "$num1 + $num2 = $sum";
}

Pretty much we say if $num3 is not = (!=) to "" (noting) do this, else do that. You could also do this the oposite way, if $num3 is = (==) to "" (nothing) do this, else do that. Its just a personal preference.

Now to call this function, all we do is add a thrid variable when we call it (in math.php), like this:

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

Now an example of what this would return.

we do this:

$sum = $obj_math->add("2", "4", "4");
we get this:
(2 + 4) x 4 = 24

we do:

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

and get

2 + 4 = 6

Thats all for now. stay tuned, next time Im going to show how to build a image upload/resize class.

This article has been taken from webdesign.org.

Tips Centre 2008-2021. All rights reserved. Developed by vrPakistanis Technologies.