Quick and dirty objects in PHP
If you have taken the opportunity to read more than a single post on this blog, it should come as no surprise that my two favourite programming languages are Ruby and JavaScript - almost completely because of the strong object-orientation of the languages. However, PHP is the bread-winner in this family of one and my lust for all things objective - syntactically speaking - has kept my mind busily crafting ways of bending PHP to my will.
One of the (many) reasons I enjoy using these languages is that they both possess the ability to create objects without having to define classes and instantiate them. For instance, consider this regular Ruby object:
class MyClass
def initialize
@my_first_var = 'Hello'
@my_second_var = 'World'
end
end
my_initialised_object = MyClass.new
Which could more easily be written like so:
{
:my_first_var => 'Hello',
:my_second_bar => 'World'
}
This syntax makes it very easy for us to do one-liners like this:
{ :one => 1, :two => 2 }.has_key?(:one)
Now, Ruby bofins will point out that what I just created is actually a Hash - but, a Hash, like everything else in Ruby, is in fact an object and can thus have methods called against them… Let’s move on shall we?
For the fun of it, let’s create those same two (original) objects in JavaScript:
function MyClass() {
this.my_first_var = 'Hello';
this.my_second_var = 'World';
}
var my_initialised_object = new MyClass();
And in short-hand form:
{
my_first_var: 'Hello',
my_second_var: 'World'
}
These short-hand methods of creating objects come in handy quite regularly in both languages and there has been many times when I have cursed PHP for it’s lack of a similar syntactic-sugar. For illustrative purposes, consider the following line of ruby-code:
"A simple string".apply_formatting({ :transform_to => 'uppercase', :limit_words_to => 4 })
Short, sweet, clean and self-documenting - smells like beautiful code to me.
While working on Musicadium.com this past week I realised that PHP does provide a way of creating short-hand objects and the solution is actually something I have written about before on this very blog. Sadly, even after writing that article I failed to connect the dots between an age old PHP feature and the problem I’m talking about here.
Before I unveil this little gem though, let’s create a regular object in PHP, to continue the nice little theme I have going:
class MyClass {
var $my_first_var = 'Hello';
var $my_second_var = 'Hello';
}
$MyClass = new MyClass;
Now, watch closely or you’ll miss the silver-bullet:
$my_initialised_object = (object) array('my_first_var' => 'Hello', 'my_second_var' => 'World');
My friend; the cast. It is a shame that I should have to resort to casting an array - which is a little slow from a performance perspective - but it is a necessary sacrifice for my coding sanity.
Casting has once again come to the rescue and made my daily development just that little bit sweeter. Unfortunately, PHP still doesn’t allow me to perform methods on that object - as with the Ruby example - but it’s a small step closer to my object-oriented dream.