A few weeks ago,
PHP 5.3 RC1 has been released and I expect the final version to be out in no more than a month. I'm watching its development since the beginning of 2008 and am really eager to see it in production. The reason for this impatience is that it brings really cool features to the language and I'd like to enumerate some of them in order of my preferences. Initially I also wanted to briefly describe them, but apparently there was to much to talk about in just one post, therefore each feature in the list below will, eventually, point to a more detailed post about the respective feature. So here's what I like best:
- Lambdas and closures
- Callable classes
- Namespaces
- The Phar extension
- Late static binding
There are some other features beside the above mentioned, but these are easier to present so I'll shortly describe them here:
1. __callStatic
This is just like the __call we all know except it is designed to work when calling methods in a static context:
<?php
class Test
{
public static function __callStatic($method, $args)
{
return array($method, $args);
}
}
var_dump(Test::dummyMethod('with dummy argument'));
2. NOWDOC strings
We all know
HEREDOC strings and the fact they are parsed by the PHP engine for variable interpolation. They're also good when we have large strings with both single and double quotes, because it saves us from escaping them. Some PHP internals thought though, that the overhead produced for variable interpolation is to big, so they came up with
NOWDOC strings. This is just like HEREDOC save for the variable interpolation. Here's an example:
<?php
$here_doc = <<<STR
Some random string with $variable interpolation...
STR;
$now_doc = <<<'STR'
Some random string without $variable interpolation...
STR;
3. Ternary shortcut (?:)
This is just like the old ternary operator except that we can leave out the middle statement.
The rule is that, if the left-hand side expression evaluates to true, its result is returned, otherwise it will return the result of the result of right-hand side expression:
<?php
var_dump('' ?: 'there was an empty string');
var_dump('I am not empty' ?: 'there was an empty string');
4. Limited goto
Honestly, I don't yet understand why this construct is limited because I have never worked with gotos. All I know is that we cannot use goto statements inside loops, this will cause a fatal error to be thrown. Anyway, here's a
basic example taken straight from the manual:
<?php
goto a;
echo 'Foo';
a:
echo 'Bar';
4 comments:
I hate "goto"s...They only provide an easier way to screw things up!
Hehe, goto's are back... Reminds me of the good old days of the Basic...
goto ICantBelieveImUsingAGoto;
:D
Goto's are really useful when building parsers/scanners.
Indeed, you can run into the danger of writing spaghetti code, BUT it CAN be done right
And yes, that can also be done easily in PHP, see
PHP_*Generator
lol, goto... long time not see, i remember from basic and batch, but on my opinion, this is usless, it scramble your code and is hard to debug...
Post a Comment