static keyword in php

What is static keyword?

static keyword is mainly used for memory management.
static keyword can be variable, method and class.

static variable

Why use static variable?
You know static keyword is useful for memory management, if we want information of the employees of TCS then we need information (i) emp_id (ii)emp_name (iii)company_name (iv)emp_designation etc.
In this fields company_name is same for all employees so we can use static.
memory_manage_static_variable

Suppose there are 10000 employees in TCS, now all instance data members(variables) will get memory each time when object is created.All employee have its unique emp Id and empname, emp designation so instance data member is good.Here, Company refers to the common property of all objects.If we make it static,this field will get memory only once.

<?php
class employee{
 public $emp_id;
 public $emp_name;
 public $emp_desg;
 public static $emp_comp="TCS";
 public function __construct($emp_id,$emp_name,$emp_desg){
 	$this->emp_id=$emp_id;
	$this->emp_name=$emp_name;
	$this->emp_desg=$emp_desg;
	 }
 public function emp_details()
 {
 echo $this->emp_id.' '.$this->emp_name.' '.$this->emp_desg.' '.self::$emp_comp;
 }
}
$emp1 = new employee(1,"john","prg");
$emp1->emp_details();
$emp2 = new employee(2,"tony","Sr.prg");
$emp2->emp_details();
?>
Output:- 1 john prg TCS
2 tony Sr.prg TCS

Without static variable counter program

In this example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won’t reflect to other objects. So each objects will have the value 1 in the count variable.
without_static_variable

<?php
class employee {
	public $count = 1;
	public function __construct() {
		echo $this -> count++;
	}

}

$emp1 = new employee();
$emp2 = new employee();
?>
Output:- 1
1

With static variable counter program

static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.
with-static-variable

<?php
class employee {
	public static $count = 1;
	public function __construct() {
		echo self::$count++;
	}

}
$emp1 = new employee();
$emp2 = new employee();
?>
Output:- 1
2

static method

You can create your function or method static using static keyword. You can access all visible static methods for you using scope resolution operator(::) like in static variables.
Note:-
(i)A static method belongs to the class rather than object of a class.
(ii)A static method can be invoked without the need for creating an instance of a class.
(iii)static method can access static data member and can change the value of it.

Why use static method?
I have instantiable classes, but don’t need an entire object, just a particular method.
example
I have a class, which has dozen of method but i need only a single method then i define this method is static.

<?php
class student {
	public $name;
	public $age;
	public static $rollno = 1125;

	public function stuInfo() {
		return $this -> name . " is " . $this -> age . " years old";
	}

	public static function ValidateRollNo($rollnumber) {
		if ($rollnumber == self::$rollno)
			return true;
		else
			return false;
	}

}

$rollnumber = 1125;
if (student::ValidateRollNo($rollnumber))
	echo "RollNumber is correct!";
else
	echo "RollNumber is NOT correct!";
?>
Output:- RollNumber is correct!

static method cannot access non-static variables and methods

Example:- In this eg. variable $name and $age which is not static, it is called into studentRecord() method which is static, when this program run it show error.

<?php
class student {
	public $name="john";
	public $age="28";
	public static $rollno=1120;

	public function stuInfo() {
		return $this -> name . " is " . $this -> age . " years old";
	}

	public static function studentRecord() {
		echo $this->name;
		echo $this->age;
		echo $this->stuInfo();
	}

}
student::studentRecord();
?>
Output:- : Using $this when not in object

static method can access static data member and can change the value of it

Example:- In this eg. static method companyinfo() is accessing the static data member $comp_name and changed the value from TCS to Infosys.

<?php
class employee {
	public $name = "john";
	public $age = "28";
	public static $comp_name = "TCS";

	public static function companyinfo() {
		self::$comp_name = "Infosys";
	}

	public function empInfo() {
		echo $this -> name . ' ' . $this -> age . ' ' . self::$comp_name;
	}

}

$emp_obj = new employee;
$emp_obj -> companyinfo();
$emp_obj -> empInfo();
?>
Output:- john 28 Infosys
  • gaurav tyagi

    this is a good material to learn about static keyword. thanks again