Re: Beginner Tutorials for using CLASSES in PHP4

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

 



All your examples all falling on deaf ears. The fact of the matter is that 
encapsulation does NOT mean data hiding, therefore I do NOT have to make all 
my member variables private.

The fact that you have used interfaces in that example proves nothing to me. 
It is possible to code a solution which does not use interfaces, so what are 
the benefits of the "with interfaces" solution over the"without interfaces" 
solution. If there are no benefits then I prefer to stick with the "without 
interfaces" solution.

Just because you CAN use interfaces does not mean that you MUST use 
interfaces. I can build solutions without them, so I do not see any 
advantage in using them.

-- 
Tony Marston
http://www.tonymarston.net
http://www.radicore.org

""Nathan Nobbe"" <quickshiftin@xxxxxxxxx> wrote in message 
news:7dd2dc0b0710100950r482cec4fi5632fd5adf345e0d@xxxxxxxxxxxxxxxxx
> 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
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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