11Sep 2008
Awesome PHP updates!
I was browsing through PHP.net's news archive today to get the scoop on any new developments and I found this article for the upcoming 5.3 release. It looks like we're going to be getting the following goodies soon:
- Namespaces
- Late static bindings
- Lambda functions / closures
- New native MySql driver MySQLnd
- A few new extensions: intl, phar, fileinfo and sqlite3
- New DIR constant
- Native GOTO-like functionality
- A good few more intrinsic functions: date_add(), date_sub(), date_diff()
- Some language improvements
I'll be discussing some of these right.... NOW!
Namespaces
This is a biggy and was supposed to be implemented when PHP 5 first came out, but they pulled it at the last minute for reason I can't seem to recall. Simply put, namespaces allow you to create little 'containers' where you can keep things like classes without having to worry about conflicting names. This allows you to shorten class names so they don't interfere with others. This is especially useful because a lot of developers are forced to come up with creative class names so they don't conflict with others. To be honest, I hate having to put a prefix on some of my classes. This is currently the way I handle namespacing in separate applications within the upcoming Cogsworth framwork.
For example, Application A might have a controller named 'MainControllerClass' and Application B may have a separate controller with the same name. Well, if we try to do a cross-application controller link - a neat Cogsworth feature - we would get a nasty PHP error for conflicting class names. So, to solve this, you have to require an application prefix for all core classes within an application. If the namespace for Application A is 'blog', then all controller class names would follow this naming convention:
<?php
class blogMainControllerClass { /* business logic goes here */ }
?>
Anyhow, back to the topic at hand. As most of you know, PHP likes to throw a nasty error on the screen if you have something like the following:
foo.class.php:
<?php
class OmgWtfBbq
{
public function __construct()
{
echo 'foo!';
}
}
?>
bar.class.php:
<?php
class OmgWtfBbq
{
public function __construct()
{
echo 'bar!';
}
}
?>
index.php:
<?php
include_once 'foo.class.php';
include_once 'bar.class.php';
$Foo = new OmgWtfBbq;
$Bar = new OmgWtfBbq;
?>
This would produce:
Fatal error: Cannot redeclare class OmgWtfBbq in bar.class.php on line 3';
Namespaces allow you to keep the same names while separating them in their own little areas. So, with namespacing, the above code would look like:
foo.class.php:
<?php
namespace HAZ;
class OmgWtfBbq
{
public function __construct()
{
echo 'foo';
}
}
?>
bar.class.php:
<?php
namespace BURGER;
class OmgWtfBbq
{
public function __construct()
{
echo 'bar!';
}
}
?>
index.php:
<?php
include_once 'foo.class.php';
include_once 'bar.class.php';
$Foo = new HAZ::OmgWtfBbq;
$Bar = new BURGER::OmgWtfBbq;
?>
Result:
foobar!
Lambda Functions (or Closures):
If you've used javascript before, especially with jQuery or Prototype, you've probably use these without even realizing it. Does this look familiar?
<script type="text/javascript">
var roflcopter = function() { alert('lolorz!'); }
</script>
Well, now you can create inline functions as well in PHP:
<?php
$roflcopter = function() { die('lolorz!'); }
$roflcopter();
?>
You can basically do the same with the create_function, but this is a much cleaner, and welcomed, method.
Another addition to this is the inclusion of the class Closure, so you can do stuff like:
<?php
$roflcopter = function() { die('lolorz!'); }
function foobar(Closure $roflcopter)
{
// Stuff goes here ... probably code of some sort.
}
?>
Also, the PHP guys added a new magic method __invoke which allows you to use classes as enclosures too:
<?php
class OmgWtfBbq
{
public function __invoke()
{
echo '(_)_)|||||||||}'; // <-- LOL!!!
}
}
$Lol = new OmgWtfBbq;
$Lol();
?>
Phar (PHP Archive):
This is a pretty sweet addition even though I don't see myself using it anytime soon. PHARs are PHP's equivalent to Java's JAR files. It allows you to compress and wrap up an entire PHP application into a single 'executable'. This way you can simply drop entire applications into your website and 'include' them as you would any file:
<?php
// Including a normal file:
include 'path/to/a/file/that/should/probably/exist/on/your/website/or/personal/computer/poop.php';
// Including a PHAR:
include 'phar:///myphar.phar/index.php';
?>
That last example allows you to open a phar stream as you would with any other type. If the package exists, you can access the contained files as you would within a directory.
GOTO:
This is something I haven't seen since I did qBasic programming in school. I can't find a whole lot of documentation on this mysterious new feature, but I did find some copy within a PHP conference meeting minutes document. From what I can tell, they're not using the traditional GOTO line_number syntax, but are repurposing the current break keyword to skip to a static label of some kind:
<?php
$things_i_like_to_ride = array('car', 'bike', 'skateboard', 'rocketsled', 'jetpack', 'batmobile', 'your mom');
foreach($things_i_like_to_ride as $thing)
{
if($thing == 'your mom')
{
break haha;
}
echo "{$thing}<br />";
haha:
echo 'definitely NOT your mom';
}
?>
Instead of printing 'your mom', it prints 'definitely NOT your mom' instead because I told it to 'GOTO' that block. Naturally, you probably wouldn't need use this feature for something as menial as the above example, but I can see some usefulness coming out of it.
ifsetor(); Functionality:
This is something that's been bothering me for a while. For something like:
<?php
echo isset($_GET['blah']) ? $_GET['blah'] : "'blah' has not been set.";
?>
I shouldn't have to reference $_GET['blah'] twice for something like this, but now I can do the following:
<?php
echo isset($_GET['blah']) ?: "'blah' has not been set.";
?>
If $_GET['blah'] exists, it will return it's corresponding value. If not, it will print out 'blah' has not been set.. Mmmmmmm... syntactic sugar!
So... yeah. :|
PHP may be a bit behind the more advanced than the more seasoned programming languages out there, but it's improvements like these that show how dedicated the developers are. Thanks for all the good stuff, guys!
I'm so excited for namespaces and closures. I use these all the time in Javascript so I'll almost certainly be integrating them into workflow to save space on simple processing – Javascript has made me want to do all my coding on one line!
I was a little unsure of how namespaces would work in PHP (I hadn't read anything until now, so was merely speculating internally!) but I think I'm going to really like how that works too!
Cheers for your article. Love your layout as well.