Recipe 7.17. Defining Static Properties and Methods


7.17.1. Problem

You want to define methods in an object, and be able to access them without instantiating a object.

7.17.2. Solution

Declare the method as static:

class Format {     public static function number($number, $decimals = 2,                                   $decimal = ',', $thousands = '.') {         return number_format($number, $decimals, $decimal, $thousands);     } } print Format::number(1234.567); 1,234.57

7.17.3. Discussion

Occasionally, you want to define a collection of methods in an object, but you want to be able to invoke those methods without instantiating a object. In PHP 5, declaring a method static lets you call it directly:

class Format {     public static function number($number, $decimals = 2,                                   $decimal = ',', $thousands = '.') {         return number_format($number, $decimals, $decimal, $thousands);     } } print Format::number(1234.567); 1,234.57

Since static methods don't require an object instance, use the class name instead of the object. Don't place a dollar sign ($) before the class name.

Static methods aren't referenced with an arrow (->), but with double colons (::)'this signals to PHP that the method is static. So in the example, the number( ) method of the Format class is accessed using Format::number( ).

Number formatting doesn't depend on any other object properties or methods. Therefore, it makes sense to declare this method static. This way, for example, inside your shopping cart application, you can format the price of items in a pretty manner with just one line of code and still use an object instead of a global function.

Static methods do not operate on a specific instance of the class where they're defined. PHP does not "construct" a temporary object for you to use while you're inside the method. Therefore, you cannot refer to $this inside a static method, because there's no $this on which to operate. Calling a static method is just like calling a regular function.

PHP 5 also has a feature known as static properties. Every instance of a class shares these properties in common. Thus, static properties act as class-namespaced global variables.

One reason for using a static property is to share a database connection among multiple Database objects. For efficiency, you shouldn't create a new connection to your database every time you instantiate Database. Instead, negotiate a connection the first time and reuse that connection in each additional instance, as shown in Example 7-37.

Sharing a static method across instances

class Database {     private static $dbh = NULL;     public function __construct($server, $username, $password) {         if (self::$dbh == NULL) {             self::$dbh = db_connect($server, $username, $password);         } else {             // reuse existing connection         }     } } $db  = new Database('db.example.com', 'web', 'jsd6w@2d'); // Do a bunch of queries $db2 = new Database('db.example.com', 'web', 'jsd6w@2d'); // Do some additional queries

Static properties, like static methods, use the double colon notation. To refer to a static property inside of a class, use the special prefix of self. self is to static properties and methods as $this is to instantiated properties and methods.

The constructor uses self::$dbh to access the static connection property. When $db is instantiated, dbh is still set to NULL, so the constructor calls db_connect( ) to negotiate a new connection with the database.

This does not occur when you create $db2, since dbh has been set to the database handle.

7.17.4. See Also

Documentation on the static keyword at http://www.php.net/manual/en/language.oop5.static.php.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net