ruk·si

🐘 PHP
Abstract Class

Updated at 2013-05-16 01:40

This note is about abstract classes in PHP. Prefer interfaces over abstract classes.

abstract class Item {
    protected $price;
    protected $name;
    public function __construct($name) {
        $this->name = $name;
        $this->price = 0;
    }
}

class Sword extends Item {
    protected $attackPower;
    public function __construct($name, $attackPower) {
        parent::__construct($name);
        $this->attackPower = $attackPower;
    }
}

class Armor extends Item {
    protected $defensePower;
    public function __construct($name, $defensePower) {
        parent::__construct($name);
        $this->defensePower = $defensePower;
    }
}

$odin = new Sword('Odin Edge', 6);
$yggra = new Armor('Yggrassil Bark', 2);
var_dump( $odin );
var_dump( $yggra );