Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What’s New in PHP 5.3
#1
PHP 6 is just around the corner, but for developers who just can't wait, there's good news -- many of the features originally planned for PHP 6 have been back-ported to PHP 5.3, a final stable release of which is due in the first half of this year.

This news might also be welcomed by those that wish to use some of the new features, but whose hosting providers will not be upgrading to version 6 for some time -- hosting providers have traditionally delayed major version updates while acceptance testing is performed (read: the stability has been proven elsewhere first). Many hosting companies will probably delay upgrading their service offerings until version 6.1 to be released. A minor upgrade from 5.2.x to 5.3, however, will be less of a hurdle for most hosting companies.

Autoloading Namespaced Classes

If you're defining the magic __autoload function to include class definition files on demand, then you're probably making use of a directory that includes all your class files. Before we could use namespaces, this approach would suffice, as each class would be required to have a unique name. Now, though, it's possible to have multiple classes with the same name.

Luckily, the __autoload function will be passed the fully namespaced reference to the class. So in the examples above, you might expect a call such as:

PHP Code:
__autoload'MyCompany::Blog::User' ); 

You can now perform a string replace operation on this parameter to convert the double colons to another character. The most obvious substitute would be a directory separator character:

PHP Code:
function __autoload$classname ) { 
       
$classname strtolower$classname ); 
       
$classname str_replace'::'DIRECTORY_SEPARATOR$classname ); 
       require_once( 
dirname__FILE__ ) . '/' $classname '.class.php ' ); 


This would take the expected call above and include the file ./classes/mycompany/blog/user.class.php.

Late Static Binding

Late static binding provides the ability for a parent class to use a static method that has been overridden in a child class. You might imagine this would be the default behaviour, but consider the following example:

PHP Code:
<?php 

class ParentClass 
    
       static public function 
say$str ) { 
               
self::do_print$str ); 
       } 

       static public function 
do_print$str ) { 
               echo 
"<p>Parent says $str</p>"
       } 



class 
ChildClass extends ParentClass 
        
       static public function 
do_print$str ) { 
               echo 
"<p>Child says $str</p>"
       } 



ChildClass::say'Hello' ); 

You would probably expect this to return "Child says Hello". While I understand why you might expect this, you'll be disappointed to see it return "Parent says Hello".

The reason for this is that references to self:: and __CLASS__ resolve to the class in which these references are used. PHP 5.3 now includes a static:: reference that resolves to the static class called at runtime:

PHP Code:
static public function say$str ) { 
               static::
do_print$str ); 
       } 

With the addition of the static:: reference, the script will return the string "Child says Hello".

__callstatic
Until now, PHP has supported a number of magic methods in classes that you'll already be familiar with, such as __set, __get and __call. PHP 5.3 introduces the __callstatic method, which acts exactly like the call method, but it operates in a static context. In other words, the method acts on unrecognized static calls directly on the class.

The following example illustrates the concept:

PHP Code:
<?php 

class Factory 

       static function 
GetDatabaseHandle() { 
               echo 
'<p>Returns a database handle</p>'
       } 

       static function 
__callstatic$methodname$args ) { 
               echo 
'<p>Unknown static method <strong>' $methodname '</strong>' 
                       
' called with parameters:</p>'
               echo 
'<pre>' print_r$argstrue ) . '</pre>'
       } 


Factory::GetDatabaseHandle(); 
Factory::CreateUser(); 
Factory::CreateBlogPost'Author''Post Title''Post Body' ); 

Variable Static Calls

When is a static member or method not static? When it's dynamically referenced, of course!

Once again, this is an enhancement that brings object functionality to your classes. In addition to having variable variables and variable method calls, you can now also have variable static calls. Taking the factory class defined in the previous section, we could achieve the same results by invoking the following code:

PHP Code:
$classname 'Factory'
$methodname 'CreateUser'
$classname::$methodname(); 

$methodname 'CreateBlogPost'
$author 'Author'
$posttitle 'Post Title'
$postbody 'Post Body'

$classname::$methodname$author$posttitle$postbody ); 
You can create dynamic namespaces like so
<?
php 
require_once( 'lib/autoload.php' ); 

$class 'MyCompany::Blog::User'
$user = new $class(); 
$user->set('fullname''Ben Balbo'); 
$user->save(); 

These little touches can make your code more readable and allow you full flexibility in an object oriented sense.

MySQL Native Driver

Until version 5.3 of PHP, any interaction with MySQL usually occurred in conjunction with libmysql -- a MySQL database client library.

PHP 5.3's native MySQL driver has been designed from the ground up for PHP and the ZEND engine, which brings about a number of advantages. Most obviously, the native driver is specific to PHP, and has therefore been optimised for the ZEND engine. This produces a client with a smaller footprint and faster execution times.

Secondly, the native driver makes use of the ZEND engine's memory management and, unlike libmysql, it will obey the memory limit settings in PHP.

The native driver has been licensed under the PHP license to avoid licensing issues.

Additional OpenSSL Functions

If you've ever had to perform any OpenSSL-related actions in your scripts, (such as generating a Diffie Hellman key or encrypting content) you'll either have performed this operation in user-land, or passed the request to a system call.

A patch to the OpenSSL functionality in PHP 5.3 provides the extra functions required to perform these actions through the OpenSSL library, which not only makes your life easier and your applications faster, but allows you to reuse the proven code that comes with OpenSSL.

This will be great news for anyone that's currently working with OpenID.

Improved Command Line Parameter Support

Hopefully, you'll be aware of the fact that PHP is more than just a scripting language for the Web. The command line version of PHP runs outside of the web server's environment and is useful for automating system and application processes.

For example, the getopt function of PHP has been around for a while, but has been limited to a number of system types; most commonly, it didn't function under a Windows operating system environment.

As of PHP 5.3, the getopt function is no longer system dependent.

XSLT Profiling

XSLT is a complex beast, and most users of this templating mechanism will be familiar with xsltproc's profiling option. As of PHP 5.3, you can profile the transforms from within your PHP scripts.

PHP Code:
$doc = new DOMDocument(); 
$xsl = new XSLTProcessor(); 

$doc->load('./lib/collection.xsl'); 
$xsl->importStyleSheet($doc); 

$doc->load('./lib/collection.xml '); 
$xsl->setProfiling("/tmp/xslt-profiling.txt"); 
echo 
$xsl->transformToXML($doc); 

echo 
'<h2>Profile report</h2>'
echo 
'<pre>' file_get_contents'/tmp/xslt-profiling.txt' ) . '</pre>'

The information produced by the profile will look something like this:

number match name mode Calls Tot 100us Avg
0 collection 1 4 4
1 cd 2 1 0

Total 3 5


New Error Levels
PHP is certainly a language that has a few, er, quirks. For example, why doesn't E_ALL include all error levels?

Well now it does! Yes, PHP 5.3 now includes E_STRICT as part of E_ALL.

Furthermore, while E_STRICT used to report on both the usage of functionality that might become deprecated in the near future, and on bad programming practices such as defining an abstract static method, in PHP 5.3, these two errors are split into E_DEPRECATED and E_STRICT respectively, which makes a lot more sense.

Other Minor Improvements

There's a handful of other improvements coming in PHP 5.3 that either don't warrant an entire section in this article, or were untestable at the time this article was written, such as:

• Sqlite3 support via the ext/sqlite extension
• SPL's DirectoryIterator, which now implements ArrayAccess
• Two news functions: array_replace and array_replace_recursive. While these functions were undefined when tested under PHP 5.3.0, the C code that implements them suggests that they will contain similar functionality to array_merge. One exception, however, is that the array_replace function will only update values in the first array where the keys match in both arrays. Any keys that are present in the second array but don't appear in the first will be ignored.

Summary

PHP 5.3 contains much functionality that was originally slated for inclusion in PHP 6, which takes it from being a minor upgrade to a significant release that every PHP developer should start thinking about. We touched on some of the new features in this blog, and looked at some code that demonstrates how you might go about using these new features.
Support
Webnetics UK Ltd.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)