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

A Singleton

One advantage of the static keyword is how easy it makes the implementation of the Singleton pattern:

       
<?php        
class Singleton {        
   /**        
   * The singleton instance is stored here        
   */        
   static private $instance = false;        
       
   private $text = 'Empty message';        
       
   /**        
   * Make the constructor private to prevent the class being        
   * instantiated directly        
   */        
   private function __construct() {}        
       
   /**        
   * Use this static method to get a singleton instance        
   */        
   static function instance() {        
       if(!Singleton::$instance) {        
           Singleton::$instance = new Singleton();        
       }        
       return Singleton::$instance;        
   }        
       
   function setText($text) {        
       $this->text = $text;        
   }        
       
   function getText() {        
       return $this->text;        
   }        
}        
       
class Hello {        
   function __construct() {        
       $single = Singleton::instance();        
       $single->setText('Hello World!');        
   }        
}        
       
class Goodbye {        
   function __construct() {        
       $single = Singleton::instance();        
       $single->setText('Goodbye World!');        
   }        
}        
       
$single = Singleton::instance();        
       
echo ( $single->getText().'<br />' );        
       
$hello = new Hello();        
       
echo ( $single->getText().'<br />' );        
       
$hello = new Goodbye();        
       
echo ( $single->getText().'<br />' );        
?>        

Script: static_singleton.php

When you run this script, you'll notice that although I call the same method via the same object reference each time ( $single->getText() ), constructing the Hello and Goodbye classes modifies the output. This is because they're all working with the same copy of the object (a Singleton). Singleton's can be valuable when you have a class that many objects need to use (perhaps a database connection) but you don't want to have multiple instances of the object (e.g. you don't want to connect to the database 10 times). Having the static keyword allows PHP developers to standardize Singleton classes, rather than having to resort to the workarounds required in PHP4.

Final

The final keyword, for which there was basically no workaround in PHP4, allows you to prevent a class member variable or method from being over-ridden in a subclass. For example:

       
<?php        
class Father {        
   final function youreGrounded() {        
       return 'Do your homework!';        
   }        
}        
       
class Child extends Father {        
   # Fatal error: Cannot override final method father::youregrounded()        
   /*        
   function youreGrounded() {        
       return 'Outta here!';        
   }        
   */        
}        
       
$child = new Child();        
       
echo ( $child->youreGrounded().'<br />' );        
?>        

Script: final.php

Attempting to override a method declared as final results in a fatal error.

Const

The keyword const allows you to declare constants within a class, which means, for example, that they can be accessed statically. With PHP4, constants could not be stored in classes; rather, all existed in the global namespace (which could result in conflicts). The following example should give you a clue as to how this can be useful:

       
<?php        
class Config {        
   const host = 'localhost';        
   const user = 'harryf';        
   const pass = 'secret';        
}        
       
mysql_connect(Config::host,Config::user,Config::pass);        
?>        

Script: const.php

References, Cloning and Dereferencing

In PHP4, references were a subject of confusion, the default behaviour, when passing objects around, being to copy rather than reference the object.

With PHP5 the default behaviour is now to pass objects by reference (using the same approach as Java, in other words). What does this mean to you, the developer? Well, if you didn't understand how references worked in PHP4, you can now pretty much forget the subject completely.

A note for those aware of the problem with foreach{} loops and object references: this has now also been solved.

Be warned, though—normal variables (i.e. those which aren’t objects) are still copied (from my experiments with the beta), so if you need to pass one by reference you still need to use the & reference operator.

Cloning

When you need a copy of an object, the __clone() method can be used. The following example should give you an idea of cloning.

       
<?php        
class Message {        
   private $text='Empty message';        
   function setText($text) {        
       $this->text = $text;        
   }        
   function getText() {        
       return $this->text;        
   }        
   function __clone() {        
       return $this;        
   }        
}        
       
$msg1 = new Message();        
$msg1->setText('This is message 1');        
echo ( 'From $msg1: '.$msg1->getText().'<br />' );        
       
$msg2 = $msg1;        
$msg2->setText('This is message 2');        
       
$msg3 = $msg1->__clone();        
$msg3->setText('This is message 3');        
       
echo ( 'From $msg2: '.$msg2->getText().'<br />' );        
echo ( 'From $msg1 again: '.$msg1->getText().'<br />' );        
echo ( 'From $msg3: '.$msg3->getText().'<br />' );        
?>        

Script: clone.php

The output of the above script looks like this:

       
From $msg1: This is message 1        
From $msg2: This is message 2        
From $msg1 again: This is message 2        
From $msg3: This is message 3        

Because $msg3 is a clone of $msg1, it is unaffected by change to the $text member variable that happened here when $msg2->setText() was called.

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

Sponsored Links