Arieh.co.il

OOP Tutorial Part 3

Welcome Back

This is the last post in our 3-part tutorial to OOP in PHP. In the previous posts we've created our Seed, which was used by our Tree class to grow. In this post, we are going to create our Garden class.  This class will receive a Seed, plant it, and generate us with a series of new trees to choose from.

This is also the only place we are going to use HTML, so that we can create a usable interface.

The Garden Class

As mentioned before, this class won't do a lot of work. It will receive a Seed, and a set of dimensions (how many columns and rows of trees to plant) and will use them to generate a Garden.

So, our constructor will be:

<?php
class Garden{
    private $tree;
    private $seed;
    private $cols = 3;
    private $rows = 3;
    
    public function __construct(SeedI $seed,$rows=3, $cols = 3){
        $this->cols = $cols;
        $this->rows = $rows;
        $this->seed = $seed;
        $this->tree = new Tree($seed);
    }
}

Not very much to see. You might notice that I broke one of my rules – I let one class create another. This is indeed a breach in our Garden's independency, but I found it more elegant that the Garden will receive a Seed, rather than receiving a Tree. Also, with our current implementation, the Tree cannot return its seed to its user. Again – this was a design decision that we could have changed, but I found it to be a nice representation – you can't ask a tree to give you back his seed now can you?

Anyway, on to growing our Garden!

Garden::Grow

The grow method is quick and dirty. Simple an HTML table generator:

public function grow(){
        ?>
        <table>
            <thead>
                <tr>
                    <th colspan="<?php echo $this->cols?>">

                        <img src='tree.php?t=<?php echo $this->seed->serialize();?>' height='300' width='300' alt='main tree' />
                    </th>
                </tr>
            </thead>
            <tbody>

            <?php for ($i=0;$i<$this->cols;$i  ):?>
                <tr>
                <?php for ($j=0;$j<$this->rows;$j  ):?>
                    <td>
                        <?php $seed =$this->tree->giveSeed(); ?>

                        <a href='garden.php?t=<?php echo $seed->serialize();?>'>
                            <img src='tree.php?t=<?php echo $seed->serialize();?>' height='300' width='300' at='plant' /></a>
                    </td>
                <?php endfor;?>

                </tr>
            <?php endfor?>
            </tbody>
        </table>
        <?php
    }

Now our interface is revealed – we are going to have 2 main files in our public library –

  1. garden.php – this will be the Garden's file. It can recieve a parameter through GET - t – which will hold a serialized version of a Seed. It will then take that DNA and plant it, giving us a list of new Trees to choose from.
  2. tree.php – this file will also be able to receive dna through GET, but it will generate a tree image.

What will happen is that our user will enter a Garden, and choose a Tree – that will link him to a Garden of the Tree's Seed. This process can be repeated indefinitely.

2 nice bonuses to using the browser for the job- one is that we get the full benefits of the browser's history – we can navigate through it, we can bookmark trees we like.

The second is that if we don't like our current Garden, we can simply refresh, and we will get a new set of trees.

Links

JavaScript Reference, JavaScript Guide, JavaScript API, JS API, JS Guide, JS Reference, Learn JS, JS Documentation