PHP Static Variable and Methods


 

PHP Static Variable and Methods

In this tutorial we will study when we should use static methods and variables. Static keyword is used to make only one copy of the variable for the class. If we declare a method as static then we can access the method using class, without instantiating any object.

In this tutorial we will study when we should use static methods and variables. Static keyword is used to make only one copy of the variable for the class. If we declare a method as static then we can access the method using class, without instantiating any object.

PHP Static Methods and Variables:

In this tutorial we will study when we should use static methods and variables.

Static keyword is used to make only one copy of the variable for the class. All objects of that class will access one copy of that attribute. Static data are used when some data has to share within the all objects of that class.

If we declare a method as static then we can access the method using class, without instantiating any object.

Example of PHP Static Variables & Methods :

<?php

class One{

private static $var=0;

function __construct(){

echo "Inside constructor";

self::$var++;

print self::$var;}

static function disp(){

print self::$var;}}

One::disp();

?>

 

Output:

0

 

 

Ads