Static Data Members :

To define a static member or method you need to prefix the class member name with the keyword 'static'.

A data member that is commonly available to all objects of a class is called a static member. Unlike regular data members, static members share the memory space between all objects of the same class.

example:
	<?php
	class Customer {
		private $first_name; // regular member
		static public $instance_count; //static data member
	}
	?>
Description :

In the above example $instance_count is declared as a static data member.

Accessing Static Data Members :

example:
	<?php
	class Customer {
		static public $instance_count = 0; //static data member
		public function __construct() {
			Customer::$instance_count++;
		}
		public function __destruct() {
			Customer::$instance_count--;
		}
		public function getFirstName() {
			//body of method
		}
		static public function getInstanceCount() {
			//body of method
		}
	}
	$c1 = new Customer();
	$c2 = new Customer();
	echo Customer::$instance_count;
	?>
OUTPUT : 2
Description :

In the above example, $instance_count is a static data member. Every time a new object is created the constructor is executed and the $instance_count variable is incremented by one. To echo the value contained in $instance_count variable, we use the :: (scope resolution) operator.

Static Methods :

A static method is a class method that can be called without creating an instance of a class. Such methods are useful when creating utility classes.

example:
    <?php
    class Customer {
	    public function getFirstName() {
		    //body of method
	    }
	    static public function getInstanceCount() {
		    //body of method
	    }
    }
    ?>
Description :

In the above example getInstanceCount is declared as a static method.

Accessing Static Method :

A static method can be accessed using the name of the class along with the scope resolution operator (::) i.e. you donot need to create an instance of that class. However, you can also access it with an instance variable.

example:
	<?php
	class Customer {
		static public $instance_count = 0; //static data member
		public function __construct() {
			Customer::$instance_count++;
		}
		public function __destruct() {
			Customer::$instance_count--;
		}
		public function getFirstName() {
			//body of method
		}
		static public function getInstanceCount() {
			return Customer::$instance_count;
		}
	}
	$c1 = new Customer();
	$c2 = new Customer();
	echo Customer::getInstanceCount(); //this is using the scope resolution operator
	echo $c1->getInstanceCount(); //this is using the instance variable
	?>
OUTPUT : 2 2

Rules to keep in mind for static methods :

1.A static method can only access static data members
2.A static method does not have access to the $this variable

Questions for Static

  1. What is static data member?
  2. What is static methods?
  3. How to define static data members in PHP5?
  4. How to define static methods in PHP5?
  5. How to accessing static data members in PHP5 with example?
  6. How to accessing static data members in PHP5 with example?
After editing Click here:
Output:
Hello World