-
-
class counter - {
- private static $count = 0;
function __construct()
- {
- self::$count++;
- }
function __destruct()
- {
- self::$count--;
- }
function getcount()
- {
- return self:: $count;
- }
- }
//Create the first instance
- $c = new counter();
//Output 1
- print( $c->getcount() . "
- n");
//Create a second instance
- $c2 = new counter();
print($c->getcount() . "
- n");
//Destroy the instance
- $c2 = null;
- < ;p> //Output 1
- print($c->getcount() . "
- n");
- ?>
-
Copy code
Note:
When a new instance is created, memory is prepared to store all properties. Each instance has its own unique set of properties.
But methods are shared by all instances of the class.
|