Tuesday, September 28, 2010

static variable in PHP vs static variable in Java

PHP is evolving to be a Object Oriented language. As a java programmer and PHP programmer, I like to compare PHP with Java. In this post, I am going to write down my comparison about "static" modifier in PHP and Java.

PHP and Java as they have different architecture for running time environment. I feel it is necessary point out the difference between PHP static and Java static. We can investigate this from two different aspects at least: static variable scope and static variable access.

Firstly, let's see the difference about static variable scope. For Java, a static variable will survive throughout the whole life when JVM is running or when class is unloaded using some techniques. That means, once the static variable is used, it will exist in memory as long as the java application is running. And, there is only one copy of static variable in memory (in one Java class loader's scope in a JVM process). For PHP, the thing is different as PHP has no memory. That means all PHP code will be flushed out after php scripts (including all included and required php script files). So, the variables, which is even declared as "static" or "Global", will be destroyed . A PHP variable can not survive through two different script executions. Other thing we need to pay attention to is that programmer is not supposed to assign a reference to a static variable.

Secondly, let's see some difference about how to access static variables in PHP and Java.  Both Java and PHP has concepts about "class"  and "instance of class" now. A "className" is used as class in code. A "new className()" is used as instance (object) of class in code. But, I noticed a difference of calling static member in class between PHP and Java. In Java, a static variable can be referred through a instance of class though it is not encouraged. In PHP, it is not allowed to do so according to my test. Below are code snippet,

 Java code
/**
 *
 * @author Yiyu Jia
 */
public class Main {

    public static String dummy = "dummy";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Main foo = new Main();
        //I can access the static variable through class instance
        //although this is not encouraged.
        System.out.println(foo.dummy); 
    }

}

 PHP code
class DummyClass
{
    static $jia = 'yiyu';

    function testStatic()
    {
     print(self::$jia);
     echo $this->jia; //error. Not allowed in PHP.
    }
}

Java and PHP has totally different run time environment architecture. I am going to find a good way to present the difference later.

For deeper understand about PHP static modifier, below URLs are very helpful,

1)Static keyword: http://php.net/manual/en/language.oop5.static.php


2)Variable scope: http://php.net/manual/en/language.variables.scope.php


3)Something important after PHP became a OOP: http://www.php.net/manual/en/language.references.return.php

2 comments:

  1. good article. thanks

    ReplyDelete
  2. Yes, in PHP you just do

    self::$var_name to reference the static variables inside the class.

    ReplyDelete