oh.i.love.spam@gmail.com wrote:
Quote:
I've been a procedural PHPer for a while now and I don't know why it
has taken me so long to start the jump from procedural to OOP. I have
a function that I would use for doing length conversions... feet to
meters, inches to centimeters, etc... and I wanted to try and turn
that into a useful class just for some OOP practice, but I don't think
it was a very good start. Maybe it was a bad function to begin
with... but I'm not seeing how this class is of any more use if any
than the function was on its own. What are the advantages of turning
that into a class and how could I improve it to make it better?
>
#################################
## Original conv function
#################################
function conv($val, $lenFrom, $lenTo) {
switch ($lenFrom) {
case 'm':
$m = $val * 1;
$cm = $val * 100;
$mm = $val * 1000;
$ft = $val * 3.2808399;
$in = $val * 39.3700787;
break;
case 'cm':
$m = $val * 0.01;
$cm = $val * 1;
$mm = $val * 10;
$ft = $val * 0.032808399;
$in = $val * 0.393700787;
break;
case 'mm':
$m = $val * 0.001;
$cm = $val * 0.1;
$mm = $val * 1;
$ft = $val * 0.0032808399;
$in = $val * 0.0393700787;
break;
case 'ft':
$m = $val * 0.3048;
$cm = $val * 30.48;
$mm = $val * 304.8;
$ft = $val * 1;
$in = $val * 12;
break;
case 'in':
$m = $val * 0.0254;
$cm = $val * 2.54;
$mm = $val * 25.4;
$ft = $val * 0.0833333333;
$in = $val * 1;
break;
}
>
switch ($lenTo) {
case 'm':
return $m;
break;
case 'cm':
return $cm;
break;
case 'mm':
return $mm;
break;
case 'ft':
return $ft;
break;
case 'in':
return $in;
break;
}
}
>
// one foot... convert it to inches
echo conv(1, 'ft', 'in'); // returns 12
>
// 24 inches... convert it to feet
echo conv(24, 'in', 'ft'); // returns 2... well, after you round
it is 2
>
>
###################################
## My attempt at converting it into a class
###################################
>
class Conv {
var $m;
var $cm;
var $mm;
var $ft;
var $in;
>
function conv($val, $lenFrom, $lenTo) {
switch ($lenFrom) {
case 'm':
$this->m = $val * 1;
$this->cm = $val * 100;
$this->mm = $val * 1000;
$this->ft = $val * 3.2808399;
$this->in = $val * 39.3700787;
break;
case 'cm':
$this->m = $val * 0.01;
$this->cm = $val * 1;
$this->mm = $val * 10;
$this->ft = $val * 0.032808399;
$this->in = $val * 0.393700787;
break;
case 'mm':
$this->m = $val * 0.001;
$this->cm = $val * 0.1;
$this->mm = $val * 1;
$this->ft = $val * 0.0032808399;
$this->in = $val * 0.0393700787;
break;
case 'ft':
$this->m = $val * 0.3048;
$this->cm = $val * 30.48;
$this->mm = $val * 304.8;
$this->ft = $val * 1;
$this->in = $val * 12;
break;
case 'in':
$this->m = $val * 0.0254;
$this->cm = $val * 2.54;
$this->mm = $val * 25.4;
$this->ft = $val * 0.0833333333;
$this->in = $val * 1;
break;
}
>
switch ($lenTo) {
case 'm':
return $this->m;
break;
case 'cm':
return $this->cm;
break;
case 'mm':
return $this->mm;
break;
case 'ft':
return $this->ft;
break;
case 'in':
return $this->in;
break;
}
}
}
>
$conv = new Conv;
>
// one foot... convert it to inches
echo $conv->conv(1, 'ft', 'in'); // returns 12
>
// 24 inches... convert it to feet
echo $conv->conv(24, 'in', 'ft'); // returns 2... well after you
round it is 2
>
################################################## #############
>
Well... I guess I can see ONE benefit.
>
When converting the 24 inches into feet above:
echo $conv->conv(24, 'in', 'ft');
>
I can go back and get millimeters for the 24 inches:
echo $conv->mm; // returns 609.6
>
or centimeters for the 24 inches:
echo $conv->cm; // returns 60.96
>
etc... without having to resend the '24' inches to the conv method in
the Conv class each time as long as the inches value doesn't change.
So essentially I can send the 24 inches once, and I've got the
converted numbers for feet, meters, centimeters and millimeters at my
disposal without having to send the 24 inches again each time.
>
What other benefits are there other than that though?
>
I'm totally green when it comes to OOP so you can totally rip that
class and tell me how you would improve it if you've got the time and
would like to share.
>
Thanks guys!
>
IBB
>
>
Essentially, not a lot of advantage for the way you have it.
Back to basics. Variables have states - they remember things. $i is a
typical variable - by itself it doesn't "do" anything.
Functions have behavior - they do things. printf() is a function - it
does stuff, but once the function call is complete, there's nothing left
from that function.
Variables don't do anything on their own (no behavior), and functions
don't remember things (typically, at least).
Objects have both state and behavior. They remember things like
variables, but can do things. The class defines the objects state and
behavior, and the object is an implementation of the class. mysqli is a
typical database object is a typical example - it can open a connection
to the database and perform operations on the database like functions,
but also remembers the database connection and other information like a
variable.
In your case, you're probably doing a lot of unnecessary conversions -
do you need every unit every time? But that doesn't mean it's not a
candidate for an object.
What I would do if I were making this an object is have functions such
as set_m(), set_cm(), set_ft(), etc. Store the value in a private
variable in whatever unit you want. Then have the equivalent get_m(),
get_cm(), get_ft(), etc. functions to retrieve the information in the
way your program needs.
And variables are part of your implementation - they should almost
always be private, occasionally protected but very seldom public.
Anything public can never be changed without potentially affecting an
unknown amount of other code - which is what OO is supposed to help limit.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================