Extending Objects - Ruby VS. PHP

Extending a base class to a more specific class is a good practice and is called "inheritance" Here's the process I go through to decide if I should make a base class and extend or not.
I make a list of the objects I will need for a game, for the VS game we have:

  • Character cards
  • Plot twist cards
  • Location cards
  • Equipment cards
  • Player
  • Board

Those items that have "card" that gives me a clue that maybe they are related and perhaps have some common attributes. Then I make a list of the attribute of each card. I'll just show two of the card types here:

Character Card

  • *Name
  • *Cost (recruit)
  • *Collector Number
  • Version
  • Team Affiliation
  • Flight
  • Range
  • Attack Value
  • Defense Value
  • *Text

Plot Twist Card

  • *Name
  • *Cost (threshold)
  • *Collector Number
  • Ongoing
  • *Text

(My apologies to the artists of the card images, I don't think I will store your name in my class at this time, although the name is printed on the card)

The attributes in common are Name, Cost (recruit cost or threshold cost), collector number and text. I then see, ahh, I could probably benefit from making a base class (it would be abstract, since I would never instantiate a basic card because there is no such card in the game)

Basic Card

  • Name
  • Cost
  • Collector Number
  • Text

In PHP you use the keyword "extends" is this manner

class CharacterCard extends Card
var $team; // string
var $hasFlight; // bool
var $hasRange; // bool
var $attack; // int
var $defense; // int

function CharacterCard($cid = "", $name = "", $text = "", $cost = 0)
parent::Card($cid, $name, $text, $cost);
$this->team = "";
$this->hasFlight = false;
$this->hasRange = false;
$this->attack = 0;
$this->defense = 0;

function dump()
print "Character Card";
print "Team: {$this->team} ATK/DEF: {$this->attack}/{$this->defense}";
if ($this->hasFlight) {
print " Flight: Yes ";

if ($this->hasRange) {

print " Range: Yes ";
} // end of class CharacterCard

Side note: Often, such php is a loosely typed language, I put comments to indicate the type of value I expect to use in that variable.

Notice in the constructor I have a call to "parent::Card" to call the parent class's constructor. Unless a subclass is meant to overwrite the parent class method, it should always call the parent class method of the same name. Even if at the time of writing the parent class, the method is empty. In the dump method, you can see I used a call to the parent dump class.

Now for Ruby, the syntax is a bit more different.

It uses a < symbol instead of a keyword, like so:

class CharacterCard < Card
def initialize(id, name, text, cost)
super(id, name, text, cost)
@team = ""
@hasFlight = false
@hasRange = false
@attack = 0
@defense = 0

def dump
puts "Team: #@team ATK/DEF #@attack/#@defense \n"
puts "Flight: #@hasFlight" unless not @hasFlight
puts "Range: #@hasRange" unless not @hasRange


To call the parent method in Ruby, you use "super". Otherwise, it looks pretty much the same. Notice the dump method, the "unless" is an interesting thing, the code before the unless happens if the "not" evaluates to true. There's no operator like that in PHP, that I know of anyways.

Next entry will deal with the ways you can do get/set methods in ruby and php.