Home Tutorial Php Phpoop PHP Static Variable and Methods

 
 

Share on Google+Share on Google+
PHP Static Variable and Methods
Posted on: February 19, 2010 at 12:00 AM
Advertisement
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

 

 

Advertisement

Related Tags for PHP Static Variable and Methods:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 19, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com