One of the key differences in the languages (Ruby vs. PHP) is the level of “object-orientation,” and (obviously) syntax. Ruby was designed from the start (~1993) to be an object-oriented (OO) language (EVERYTHING is an object) while PHP was, until recently (v5.0 – 2004), a procedural language. As a long-time PHP programmer who migrated to Ruby in 2006, I prefer Ruby’s implementation of Types and Objects over PHP's. I also prefer Ruby’s syntax AND coding best-practices over PHP’s.
Why is "Everything is an Object" in Ruby and not PHP? (I know those of you who are well-versed in OO programing may cringe at the phrase "Everything is an Object," but the phrase is sufficient for this discussion). The short answer is that PHP is "type aware" of its variables when a value is set and Ruby "Duck Types" its variables (more on "Duck Typing" later) when assigned a value and creates an Object.
Programming Objects are abstract representations of real things (example: a car). Real things have state (condition) and behavior. Using the car as an example, a car's state may consist of year, make, model, and color and its behavior can consist of accelerating, turning, and stopped. The Object's state is saved as an attribute and we access the Object's behavior through methods (or functions). For example: the 1969 red Chevrolet Camaro (attributes) turning right (behavior).
In programming languages the variable (data) Type indicates how the language will process the variable's (data) value. If the variable's Type is a number, we use mathematical operators (1+2=3), if the variable's Type is a string, we use string operators ("1"+"2"="12"), etc... PHP and Ruby are 'Dynamically Typed' languages which means the variable Types are evaluated at runtime (when the program is executed). Ruby goes a step further... during runtime Ruby creates an object for the variable based on its Type. For instance, if Ruby determines the variable is a String, it creates a String object. PHP does not create an object based on the variable's Type, though PHP is aware what the variable's Type is.
OK... back to "Everything is an Object." When we assign a value to a variable in Ruby, Ruby determines what the variable's Type is and creates a 'Type' Object. Ruby does this via "Duck Typing." To paraphrase... If it walks like a duck and quacks like a duck, we are going to call it a duck (even if it is not). You can read more about "Duck Typing" here: Wikipedia Article on the History of the Term "Duck Typing".
Here is an example of setting a value to a PHP variable and PHP's type-awareness:
php > $a = '1';
php > echo gettype($a);
string
php > echo get_class($a);
PHP Warning: get_class() expects parameter 1 to be object, string given in php shell code on line 1
php > $a = 1;
php > echo gettype($a);
integer
php > echo get_class($a);
PHP Warning: get_class() expects parameter 1 to be object, integer given in php shell code on line 1
As you can see, the variable was not created as an object, though PHP is aware what the Type of the variable is.
Now, let's do the same thing with Ruby:
ruby-1.8.7-p352 :001 > a = '1'
=> "1"
ruby-1.8.7-p352 :003 > a.class
=> String
ruby-1.8.7-p352 :004 > a = 1
=> 1
ruby-1.8.7-p352 :005 > a.class
=> Fixnum
ruby-1.8.7-p352 :006 > a.methods
=> ["%", "odd?", "inspect", "prec_i", "<<", "tap", "div", "&", "clone", ">>", "public_methods", "__send__",
"object_id", "instance_variable_defined?", "equal?", "freeze", "to_sym", "*", "ord", "+", "extend", "next",
"send", "round", "methods", "prec_f", "-", "even?", "singleton_method_added", "divmod", "hash", "/", "integer?",
"downto", "dup", "to_enum", "instance_variables", "|", "eql?", "size", "instance_eval", "truncate", "~", "id",
"to_i", "singleton_methods", "modulo", "taint", "zero?", "times", "instance_variable_get", "frozen?",
"enum_for", "display", "instance_of?", "^", "method", "to_a", "+@", "-@", "quo", "instance_exec", "type", "**",
"upto", "to_f", "<", "step", "protected_methods", "<=>", "between?", "==", "remainder", ">", "===", "to_int",
"nonzero?", "pred", "instance_variable_set", "coerce", "respond_to?", "kind_of?", "floor", "succ", ">=", "prec",
"to_s", "<=", "fdiv", "class", "private_methods", "=~", "tainted?", "__id__", "abs", "untaint", "nil?", "chr",
"id2name", "is_a?", "ceil", "[]"]
Not only is Ruby aware of the variable's Type, it instantiates an Object of the class of that type. The variable
now has access to all the methods of the Object Ruby determined the variable belonged to. You can see that by calling the .methods method on the Fixnum Object 'a' and getting a list of methods which can be called.
The reason "Everything is an Object" in Ruby is because Ruby "Duck Types" a variable when the value is assigned and creates an Object based on its Type. PHP is aware of the Type of the variable when the value is set, but does not create an Object.
We've looked at the differences between Types and Objects in Ruby and PHP. My next post will compare and contrast syntax between Ruby and PHP.
Comments
No comments yet.