Basic "bee game" labeled as messy Could you tell me how to make it quality code?

&lt;?php
class Game {
    public function __construct() {
        session_start();
    }
    public function play(){
        return '&lt;input type="button" value="Play" onclick="window.location = \'?play=1\'"&gt;';
    }
    public function start($msg=null) {
        unset($_SESSION['bees']);
        $bee = new Bee();
        $bee-&gt;setType('queen');
        $this-&gt;addBee($bee,1);
        $bee-&gt;setType('worker');
        $this-&gt;addBee($bee,5);
        $bee-&gt;setType('drone');
        $this-&gt;addBee($bee,8);
        if($msg)
            return $msg;
        else {
            var_dump($_SESSION['bees']); // debug
            return 'Game started &lt;input type="button" value="Hit" onclick="window.location = \'?hit=1\'"&gt;';
        }
    }
    public function hit() {
        $msg = '';
        if($_SESSION['bees'])
            $rd = array_rand($_SESSION['bees'],1);
        else
            $this-&gt;start('Game will start again');
        $_SESSION['bees'][$rd]['lifespan'] = $_SESSION['bees'][$rd]['lifespan'] - $_SESSION['bees'][$rd]['hit'];
        if($_SESSION['bees'][$rd]['lifespan']&lt;1) {
            $_SESSION['bees'][$rd]['bees'] -=1;
            $_SESSION['bees'][$rd]['lifespan'] = $_SESSION['bees'][$rd]['life'];
            switch($rd)
            {
                case 'queen': $msg .= $this-&gt;start('Game start again, the Queen is dead.'); break;
                default: $msg .= 'One \'' . $rd . '\' is gone';
            }
        }
        if($_SESSION['bees'][$rd]['bees']&lt;1){
            unset($_SESSION['bees'][$rd]);
            $msg = 'The \'' . $rd . '\' team is gone';
        }
        var_dump($_SESSION['bees']); // debug
        return '&lt;input type="button" value="Hit" onclick="window.location = \'?hit=1\'"&gt; ' . $msg;
    }
    public function addBee(Bee $newbee, $number = 1) {
        $tipo = $newbee-&gt;getType();
        $bee = $newbee-&gt;get();
        $_SESSION['bees'][$tipo] = $bee;
        $_SESSION['bees'][$tipo]['bees'] = $number;
        $_SESSION['bees'][$tipo]['life'] = $newbee-&gt;getLifespan($tipo);
    }
}

class Bee
{
protected $_type = null;
protected $_types = array(
    'queen' =&gt; array(
    'hit' =&gt; 8,
    'lifespan' =&gt; 100
    ),
    'worker' =&gt; array(
    'hit'   =&gt;  10,
    'lifespan' =&gt; 75
    ),
    'drone' =&gt; array(
    hit'    =&gt;  12,
    'lifespan' =&gt; 50
    )
    );
    public function setHit(){}
    public function setLifespan(){}
    public function addNewType(){}
    protected function _set_hit(){}
    protected function _set_lifespan(){}
    protected function _add_new_type(){}
    public function getLifespan($type){
        if(array_key_exists($type,$this-&gt;_types))
            return $this-&gt;_types[$this-&gt;_type]['lifespan'];
        else
            throw new Exception('The Bee need to be a Queen, Worker or Drone');
    }
    public function getTypes() {
        return $this-&gt;_types;
    }
    public function getType() {
        return $this-&gt;_type;
    }
    public function setType($type) {
        if(array_key_exists($type,$this-&gt;_types))
            $this-&gt;_type = $type;
        else
            throw new Exception('The Bee need to be a Queen, Worker or Drone');
    }
    public function get() {
        return $this-&gt;_types[$this-&gt;_type];
    }
}
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;play Bee&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;?php                                               
        $game = new Game();
        if(isset($_GET['play']) &amp;&amp; $_GET['play']) {
            echo $game-&gt;start();
        } else if(isset($_GET['hit']) &amp;&amp; $_GET['hit']) {
            echo $game-&gt;hit();
        } else {
            echo $game-&gt;play();
        }
    ?&gt;
&lt;/body&gt;
&lt;/html&gt;
One big issue with this code is that you've got a tight coupling between the business logic and the presentation.  For example, the method play() really jumps out:  

public function play(){

    return '&lt;input type="button" value="Play" onclick="window.location = \'?play=1\'"&gt;';

}


From its name I'd think that this method plays something (a game? A piece of music? A role in a play? Another issue is clear, unambiguous naming of classes, methods and variables).  I wouldn't expect it to output the HTML markup for a button with a JavaScript event attached to it.  

This might not seem like a big deal, and in this particular case it probably isn't.  But ask yourself this, how much work would you need to do if whoever set you this project decided half way though that it should run through the command line instead of a web browser? 

Ideally, your PHP classes should contain no presentation logic at all.  They should return data structures (arrays, scalar types, simple data transfer objects, etc) to the outside world, and then a separate process should take that information and render it to the user in a suitable presentation.  The classes that do the actual work (the business objects) should implement all the logic and rules needed to accomplish a task without any concern as to how the results of that task are presented.  

This concept is known as separation of concerns.  A concern is a particular part of the software package you're developing which shouldn't be tightly bound (coupled in the lingo) to other parts of your package (sorry I can't really word it better than that).  Implementing the rules and maintaining the state of the game is one concern, getting user input and displaying output to the user is a different concern.  While there obviously has to be some coupling you should try to minimize the degree that one is bound to the other.  Additionally, it's generally considered preferable for the part of the program responsible for presentation (which will typically be unique to that program) to know something about the business objects than it is for the business objects (which may get used across a wide range of applications) to know something about the presentation.  

A related issue is reliance on $_SESSION inside your class.  Again this has to do with separation of concerns.  The $_SESSION array and its associated methods (session_start (), etc) are a data storage mechanism.  Data storage is a separate concern from the business logic, so again you should try to separate the code that handles storing the game state out from the code that implements the business logic.  In the case of $_SESSION there's the additional problems of it being tightly coupled to HTTP and that the session_start () function can't be called once headers have been sent to the client.  Again, ask yourself how hard would it be to adapt your code to work in a command line or to use a database instead of the PHP session to store state?