Re: Beginner Tutorials for using CLASSES in PHP4

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



and here is the interface example youve (Tony) been asking for.
the reason an interface is the best option here is because human hands and
grass are not related,
but they can both be cut (i didnt start the fingers thing, but it works for
the example so im using it).

also, notice the benefit of composition.  the same mower can be used invoke
the cut method on
different Cuttables at runtime.
the reason this works is because Cuttable encapsulates a family of similar
algorithms.  know what
that ones called; the strategy pattern; i must be a patterns zealot.

also, regarding the, is grass part of the algorithm thing; i thought about
it more; i think this boils down to
definition vs. instantiation.  take functions for example; they have formal
and actual parameters.

function someFunction($formalParameter) {
    echo $formalParameter . PHP_EOL;
}

$actualParameter = 'Hello World';

someFunction($actualParameter);

$actualParameter is not part of the someFunction implementation, but
$formalParameter is.

in the same way (in the following example) the Grass instances are not part
of the Lawnmower implementation,
but the member variables and method signatures (of Lawnmower) do constitute
its implementation.

<?php
class Lawnmower {
    const GAS_MIN = 0;
    const GAS_MAX = 10;
    const BLADE_HEIGHT_MIN = 1;
    const BLADE_HEIGHT_MAX = 5;

    private $engineOn = false;
    private $gasLevel = 5;
    private $bladeHeight = 3;

    public function turnOn() {
        if($this->canRun()) {
            $this->engineOn = true;
        } else {
            echo 'you need to add gas to run the mower!' . PHP_EOL;
        }
    }

    public function mowGrass(Cuttable $theGrass) {
        $theGrass->cut($this->bladeHeight);
    }

    public function turnOf() {
        $this->enginOn = false;
    }

    public function addGas($amountOfGas) {
        if(($amountOfGas + $this->gasLevel) > self::GAS_MAX) {
            $this->amountOfGas += $amountOfGas;
        } else {
            echo 'you will overflow the tank w/ all that gas.' . PHP_EOL;
            return false;
        }
    }

    public function decreaseBladeHeight() {
        if($this->bladeHeight > self::BLADE_MIN_HEIGHT) {
            $this->bladeHeight--;
        } else {
            echo 'the blades are already at their lowest setting' . PHP_EOL;
        }
    }

    public function increaseBladeHeight() {
        if($this->bladeHeight > self::BLADE_HEIGHT_MAX) {
            $this->bladeHeight++;
        } else {
            echo 'the blades are already at their highest setting' .
PHP_EOL;
        }
    }

    public function checkGas() {
        return $this->gasLevel;
    }

    private function canRun() {
        $canRun = false;

        if($this->gasLevel > self::GAS_MIN) {
            $canRun = true;
        }

        return $canRun;
    }
}

interface Cuttable {
    function cut($newHeight);
}

class Grass implements Cuttable {
    const MIN_HEIGHT = 0;

    private $grassHeight = 3;

    public function grow() {
        $this->grassHeight++;
    }

    public function cut($newHeight) {
        if($newHeight < self::MIN_HEIGHT) {
            echo 'dirt flying in every direction!' . PHP_EOL;
            $this->grassHeight = self::MIN_HEIGHT;
        } else {
            $this->grassHeight = $newHeight;
        }
    }
}

class HumanHand implements Cuttable {
    private $numFingers = 5;

    public function cut($newHeight) {
        //// TODO: ROB CAN FILL IN THE PART ABOUT WHAT HAPPENS TO THE
FINGERS :)
        echo 'oh dear, i have to go to the hospital!' . PHP_EOL;
    }
}


$myLawn = new Grass();
$neighborsLawn = new Grass();
$myMower = new Lawnmower();
$myHand = new HumanHand();

// neighbors havent cut their grass in weeks
$neighborsLawn->grow();
$neighborsLawn->grow();

echo 'ill cut my grass first at the standard blade height' . PHP_EOL;
$myMower->turnOn();
$myMower->mowGrass($myLawn);

echo 'now ill adjust the blade height, because the neighbors like their
grass tall' . PHP_EOL;
$myMower->increaseBladeHeight();
$myMower->mowGrass($neighborsLawn);

echo 'bummer; there is some gunk on the underside of the mower; pehaps ill
use my hand to dig it out..' . PHP_EOL;
$myMower->mowGrass($myHand);
?>

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux