What is Php static keyword?
static keyword can be variable, method and class.
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.
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();
?>
2 tony Sr.prg TCS
Without static variable counter program
<?php
class employee {
public $count = 1;
public function __construct() {
echo $this -> count++;
}
}
$emp1 = new employee();
$emp2 = new employee();
?>
1
With static variable counter program
<?php
class employee {
public static $count = 1;
public function __construct() {
echo self::$count++;
}
}
$emp1 = new employee();
$emp2 = new employee();
?>
2
static method
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!";
?>
static method cannot access non-static variables and methods
<?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();
?>
static method can access static data member and can change the value of it
<?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();
?>
Php static keyword – Interview Questions
Q 1: What is the static keyword in PHP?
Q 2: How do static properties and methods work in PHP?
Q 3: Can static methods access non-static properties in PHP?
Q 4: How do you access static members in PHP?
Q 5: What are the advantages of using the static keyword in PHP?
Php static keyword– Objective Questions (MCQs)
Q1. static keyword is used to ______.
Q2. Static properties are accessed using ______.
Q3. Static methods can be called ______.
Q4. Static variables inside a function retain their value ______.
Q5. Can static methods access $this?