Webhosting and cheap domain registration services
  

 Home

PHP Manual
PrevChapter 18. Classes and Objects (PHP 5)Next

Static Keyword

Declaring class members or methods as static, makes them callable from outside the object context. A member or method declared with static can not be accessed with a variable that is an instance of the object and cannot be re-defined in an extending class.

The static declaration must be after the visibility declaration. For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public static.

Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.

Example 18-11. Static member example

<?php
class Foo {
  
public static $my_static = 'foo';

  
public function staticValue() {
    return
self::$my_static;
  }
}

class
Bar extends Foo {

  
public function fooStatic() {
    return
parent::$my_static;
  }
}


print
Foo::$my_static . "\n";

$foo = new Foo();
print
$foo->staticValue() . "\n";
print
$foo->my_static . "\n";      // Undefined my_static

print Bar::$my_static . "\n";
$bar = new Bar();
print
$bar->fooStatic() . "\n";
?>

Example 18-12. Static method example

<?php
class Foo {
  
public static function aStaticMethod() {
    
// ...
  
}
}

Foo::aStaticMethod();
?>

PrevHomeNext
::UpObject Constants

 

  

 

 

 Network sites:

Domain registration : Domain registration and domain name transfer services
Cheap Domain Registrar : Cheap domains or register domain name from $5.95
 

Disclaimer: This documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to http://www.php.net/docs.php