views:

105

answers:

2

what are your favorite PHP programming trick(s)?

I myself like Resession (Session Manager)

A: 

I like Variable variables.

charles.art.br
Ah, the bane! IMNSHO, variable variables are for people to lazy to create arrays, and immensely unreadable for everyone else on the project :P
Wrikken
I haven't found use for variable variables that couldn't be solved more cleanly with an array.
Alex JL
Nope, variable variables lead to messy code and should be avoided. Arrays do the same job with much less pain.
Techpriester
Variable variable is as safe as using eval.
HoLyVieR
+3  A: 

Static function cacheing:

function cachedFunction($foo)
{
    static $result = array();
    if (isset($result[$foo])) {
        return $result[$foo];
    }

    $return[$foo] = resultOfSomeTimeConsumingStuff();
    return $return[$foo];
}

If the result of the function will not change during the run of your application you can get much faster repeated calls to the cached function.

A well designed application won't need this but it's pretty useful to squeeze a lot of performance out of old or poorly engineered systems without much effort.

I've worked on project where this reduced the time for the average HTTP request to about 50%. because the app was repeatedly calling the same functions without actually needing a "fresh" result.

Just keep in mind: This trick may give you out-of-date data! So be careful.

Techpriester
Nice one! -----
Pekka