Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

wbcarts at juno dot com
14 years ago
CLASSES and OBJECTS that represent the "Ideal World"

Wouldn't it be great to get the lawn mowed by saying $son->mowLawn()? Assuming the function mowLawn() is defined, and you have a son that doesn't throw errors, the lawn will be mowed.

In the following example; let objects of type Line3D measure their own length in 3-dimensional space. Why should I or PHP have to provide another method from outside this class to calculate length, when the class itself holds all the neccessary data and has the education to make the calculation for itself?

<?php

/*
* Point3D.php
*
* Represents one locaton or position in 3-dimensional space
* using an (x, y, z) coordinate system.
*/
class Point3D
{
    public
$x;
    public
$y;
    public
$z;                  // the x coordinate of this Point.

    /*
     * use the x and y variables inherited from Point.php.
     */
   
public function __construct($xCoord=0, $yCoord=0, $zCoord=0)
    {
       
$this->x = $xCoord;
   
$this->y = $yCoord;
       
$this->z = $zCoord;
    }

   
/*
     * the (String) representation of this Point as "Point3D(x, y, z)".
     */
   
public function __toString()
    {
        return
'Point3D(x=' . $this->x . ', y=' . $this->y . ', z=' . $this->z . ')';
    }
}

/*
* Line3D.php
*
* Represents one Line in 3-dimensional space using two Point3D objects.
*/
class Line3D
{
   
$start;
   
$end;

    public function
__construct($xCoord1=0, $yCoord1=0, $zCoord1=0, $xCoord2=1, $yCoord2=1, $zCoord2=1)
    {
       
$this->start = new Point3D($xCoord1, $yCoord1, $zCoord1);
       
$this->end = new Point3D($xCoord2, $yCoord2, $zCoord2);
    }

   
/*
     * calculate the length of this Line in 3-dimensional space.
     */
   
public function getLength()
    {
        return
sqrt(
           
pow($this->start->x - $this->end->x, 2) +
           
pow($this->start->y - $this->end->y, 2) +
           
pow($this->start->z - $this->end->z, 2)
        );
    }

   
/*
     * The (String) representation of this Line as "Line3D[start, end, length]".
     */
   
public function __toString()
    {
        return
'Line3D[start=' . $this->start .
           
', end=' . $this->end .
           
', length=' . $this->getLength() . ']';
    }
}

/*
* create and display objects of type Line3D.
*/
echo '<p>' . (new Line3D()) . "</p>\n";
echo
'<p>' . (new Line3D(0, 0, 0, 100, 100, 0)) . "</p>\n";
echo
'<p>' . (new Line3D(0, 0, 0, 100, 100, 100)) . "</p>\n";

?>

  <--  The results look like this  -->

Line3D[start=Point3D(x=0, y=0, z=0), end=Point3D(x=1, y=1, z=1), length=1.73205080757]

Line3D[start=Point3D(x=0, y=0, z=0), end=Point3D(x=100, y=100, z=0), length=141.421356237]

Line3D[start=Point3D(x=0, y=0, z=0), end=Point3D(x=100, y=100, z=100), length=173.205080757]

My absolute favorite thing about OOP is that "good" objects keep themselves in check. I mean really, it's the exact same thing in reality... like, if you hire a plumber to fix your kitchen sink, wouldn't you expect him to figure out the best plan of attack? Wouldn't he dislike the fact that you want to control the whole job? Wouldn't you expect him to not give you additional problems? And for god's sake, it is too much to ask that he cleans up before he leaves?

I say, design your classes well, so they can do their jobs uninterrupted... who like bad news? And, if your classes and objects are well defined, educated, and have all the necessary data to work on (like the examples above do), you won't have to micro-manage the whole program from outside of the class. In other words... create an object, and LET IT RIP!

<< Back to user notes page

To Top