Attribute Accessors - Ruby VS. PHP

Typically in objects you don't set/get the attributes directly, you use a method. This allows you to do type checking when setting the data or to format it a certain way when getting the data. In php I would use something like this:

    function setAttack( $atk ) { $this->attack = $atk; }
    function getAttack() { return $this->attack; }

And used like this:

echo $testcard->getAttack();

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:

Objects - Ruby vs. PHP

I was teaching my husband some programming in PHP (meanwhile, I’m learning Ruby) and wanted to find something he was interested in to use for examples. He is an avid player of the game VS; and I have resigned to a "if you can't beat 'em, join 'em" mentality and started to play too (Geez, I was tired of being ignored!). It’s a pretty complex game, but kind of fun once you learn all the rules.

I found it to be a pretty good problem area for teaching. There are plenty of if/else statements to learn about conditional statements, operators and entities that can serve as objects and interaction between card, player, opponent etc.

I’ll keep it simple, there are few more attributes I could put in this class, but this suffices for illustration of Ruby and PHP.

A basic card has a name, cost, text and id string.

Ruby for the PHP Programmer ...

This will be a series of my findings in the syntax and language of Ruby from a PHP perspective.

Displaying and formatting output is one of the first things you need to know when learning a new language.


echo Look it up at
echo value [,values];
This is not a function, so ( ) is not needed. It is a language construct. Hmm, something I just learned. it will concat the variable for you and display. This might come in handy, sometimes is a pain to always do print $var1 . " " . $var2; Sometimes those periods are easy to forget!


$first = "Nola";
$last = "Stowe";
echo $first, ' ', $last, "<br>";

print Look it up at
print value;
Also not a function but a language construct, so () not needed here as well.


$first = "Nola";
$last = "Stowe";
print $first." ". $last."<br>";
// or
print "$first $last <br>"";

results in:
Nola Stowe

For more on the differences of print and echo, read:

In most cases, I usually use print.

Also availble in php, but I never rarely use:
printf (string format, [, mixed args]); uses a formatting string
sprintf (formats as printf, but reutrns a string rather than printing)
vsprintf (like sprintf, but accepts an array of values instead of a variable number)
vprintf (like printf, but accepts an array of values instead of a variable number)

(which BTW, does not require ; at end of line, local vars don't have $ those are reserved for globals)

first = "Nola"
last = "Stowe"

print first, ' ', last, "<br>"

prints contents of variable test followed by test2

results in:
Nola Stowe

However, it appears some special variables (lookie! a $!!! that means they are global)

$, = " " #displayed between variables in print
$\ = "<br>" #displayed at end of the line

Will set some defaults for the print statement, so once those are set, all you need to do is:

print first,last

To get the same result. Cool, huh?

Also in Ruby I found puts, and putc. I need to do some more investigation on those. Perhaps a reader can shed some light?

So you can see, PHP's echo is most like Ruby's print. That should help us die hard php programmers who normally use print exclusively.
Also in Ruby, which seem to work like the php versions: