Article

PHP5: Coming Soon to a Webserver Near You

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Next

Dereferencing

One final reference-related feature that PHP5 provides is dereferencing.

Dereferencing allows you to access an object via another object’s method, basically providing a short cut to reduce the number of lines of code. For example:

         
<?php          
class Factory {          
   static function create() {          
       return new Message();          
   }          
}          
         
class Message {          
   private $text = 'This is a message';          
   function getText() {          
       return $this->text;          
   }          
}          
         
echo ( Factory::create()->getText().'<br />' );          
?>          

Script: dereference.php

Using the above classes with PHP4 might look like;

         
$message = Factory::create();          
echo ( $message->getText().'<br />' );          

Although this may not look too impressive in the above example, it may prove useful for complex objects, such as when you use the DOM XML extension, saving a lot of coding.

AutoLoading

PHP, as you know, is an interpreted language where your source code has to be re-examined each time a script is executed. That's great for developers, as it allows us to bypass the long wait that comes with compiled languages, and gives us plenty of opportunities to experiment and prototype quickly. The downside is that the more code you have, the longer it takes the PHP parser to work through it all on each execution—bad news for end user performance.

Where classes are concerned, this is certainly a problem. Typically, you'll need to include all the classes that will ever be used by your application, even if the current "operation" only uses a few of them. That means the PHP parser has to do a lot of unnecessary work.

PHP5 provides a handy new function, __autoload(), thanks to Ivan Ristic, which allows you to have a class included only when it's instantiated.

For example, take the following class file:

         
<?php          
echo ( 'Message class now being autoloaded.<br />' );          
// Autoloaded          
class Message {          
   private $text = 'This is a message';          
   function getText() {          
       return $this->text;          
   }          
}          
?>          

Script: Message.php

Let’s include this file in a script that uses it:

         
<?php          
function __autoload($name) {          
   require_once($name.'.php');          
}          
         
if ( isset($_GET['autoload']) ) {          
   $message = new Message();          
   echo ( $message->getText().'<br />' );          
}          
         
echo ( 'Execution finished' );          
?>          

Script: autoload.php

You'll notice that if you view the script normally, without adding any variables to the URL, it outputs 'Execution Finished'. But appending a "?autoload" will display "Message class now being autoloaded." as well as the output from the getText() method.

No doubt there are other situations where __autoload() can come in handy, such as when you need to unserialize a stored object.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links