Share on Google+Share on Google+

PHP Static Variable and Methods

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

 

 

Advertisements

Advertisement

Posted on: February 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.