Today I want to share with you some features of PHP that I think every developer should know about. Hopefully one of them will be new to you.
Tidbit #1 – Named subpatterns in Regular Expressions
By prepending a subpattern with
?P<name> you can have any
preg_* function return the match using the specified name as the array key instead of the match’s index. Although this is not stricly a PHP feature, I find it quite useful especially when dealing with regular expressions that have multiple subpatterns.
The real advantage of using named subpatterns over numerical indexes is that the latter may change if you modify your regular expression. The day that happens you will need to go through your code and replace all your
$matches, but named subpatterns prevents this as the year will always be
Tidbit #2 – ArrayAccess, Iterator and Countable interfaces
The ArrayAccess interface allows you to access an object as if it were an array.
If you are interested in implementing ArrayAccess, make sure you take a look at its complementing interfaces: Iterator and Countable.
Tidbit #3 – Autoloading classes
The days of having to manually load all of your project’s dependencies are over. The __autoload function enables you to set up a callback that will be triggered whenever a non-existing class is referenced.
The spl_autoload_register function lets you define multiple autoload callbacks. Some open-source frameworks use this feature to ensure consistent class naming. One typical example of this is Zend Framework which uses the class name to define where it is located in the filesystem. For instance, the
Zend_Http_Client class is located in
./Zend/Http/Client.php. It also ensures that there is only one class per file.
Tidbit #4 – Anonymous Functions and Closures
Anonymous functions have always been something I felt PHP was missing. Lo and behold! They are available since PHP 5.3. An anonymous function allows you to define a function and assign it to a variable. The best sample usage I could find is from PHP’s RFC for closures:
Anonymous functions are good when you want to define a function that is only used for a specific purpose. It allows you to keep your global namespace clean.
use keyword enables you to import variables from the current scope into your function’s scope. These types of functions are called closures.
Since all anonymous functions and closures are instances of the Closure object, you can use type hinting to force a function parameter to be a be an anonymous function/closure.
Tidbit #5 – Memoizing using static keyword
There are many techniques to cache data in PHP (APC, Memcached, Redis, etc..), but in some situations this might be overkill. If you are looking for a quick and effective way to cache a function’s result the
static keyword could be the answer.
Keep in mind static variables are not shared between PHP processes, making this type of caching only useful in certain situations. Simas Toleikis has a detailed article about this approach on his blog.
If you have any questions about the features mentioned above, don’t hesitate to ask or look them up on Google.
What are your favorite PHP features?
By Christian Joudrey, CakeMail’s Lead Programmer. You can follow Christian on twitter @cjoudrey.