Nested menu with PHP’s RecursiveIteratorIterator

The Standard PHP Library (SPL) has been around for years now, yet we don’t see them being used in our daily codes. Frameworks like Symfony2, Laravel use them internally. Today I am writing about one of the key members of the SPL, the RecursiveIteratorIterator, and implement something web developers always have to deal with, nested menus.

As the name suggests, it is used to iterate through a recursive iterator. There are many recursive iterators provided in SPL like RecursiveArrayIterator and RecursiveDirectoryIterator. A recursive iterator should implement the RecursiveIterator interface. There is not much documentation about these in the PHP website, but there are examples you can learn from.

If you have never used any of the Iterators, you can think of them as ‘loopables’.  The `RecursiveIterator` interface extends `Iterator` which extends `Traversable`, which is described as “Interface to detect if a class is traversable using foreach“, meaning iterators can be directly passed to foreach and looped. Continue reading Nested menu with PHP’s RecursiveIteratorIterator

The Square Root of Three

Taken from the movie ‘Harold and Kumar Escape from Guantanamo Bay’, this is the best poem I have ever heard (being a programmer of course 🙂 )

I fear that I will always be
A lonely number like root three
The three is all that’s good and right,
Why must my three keep out of sight
Beneath the vicious square root sign,
I wish instead I were a nine
For nine could thwart this evil trick,
with just some quick arithmetic
I know I’ll never see the sun, as 1.7321
Such is my reality, a sad irrationality
When hark! What is this I see,
Another square root of a three
Has quietly come waltzing by,
Together now we multiply
To form a number we prefer,
Rejoicing as an integer
We break free from our mortal bonds
With the wave of magic wands
Our square root signs become unglued
Your love for me has been renewed

Here’s that clip in the movie 🙂

Deploying symfony2 project with capifony at hostgator shared hosting(subdomain)

I was recently working on a Symfony project and I needed to deploy it to a shared hosting at Hostgator. The default PHP version in Hostgator shared hosting is 5.2.17 (thats in mine). Some of you might know that Hostgator provides support for other versions of PHP as well (5.3 and 5.4). I use Capifony to deploy my symfony projects. So I had to made some adjustments to the deploy recipe that helped me successfully deploy a symfony2 (2.3.7) to a shared hosting in Hostgator. And I put here the full procedure to successfully setup capifony for deploy at Hostgator. (This was a staging deploy to a subdomain. If you need to deploy to your primary domain, I’m afraid Hostgator does not allow you to change the DocumentRoot for your primary domain. See this link to know more on changing DocumentRoot at Hostgator) Continue reading Deploying symfony2 project with capifony at hostgator shared hosting(subdomain)