Hello reader's today we will discuss about "static variable scope in PHP vs static variable scope in Java".
PHP is actually increasing to become a Object Orient Programing. Being a java coder in addition to PHP coder, php in addition to java while they have different architecture for running time environment.
let's start to see the difference with regards to static variable scope. Intended for Java, the static variable will certainly survive through the entire whole life as soon as JVM is running or as soon as class will be unloaded using some techniques. Which means, after the static variable is used, it will eventually exist in memory so long as this java application is running. Along with, there is only one duplicate of static variable within memory (in just one Java class loader's scope in a JVM process). For PHP, the truth is diverse because PHP doesn't have any memory. That means just about all PHP code will probably be flushed after php scripts (including just about all included along with required php script files). Thus, this variables, that's even declared "static" or "Global", will probably be destroyed. The PHP variable are not able to survive via a two different script executions. Some other thing we must take note is that programmer is not assign a reference to a static variable.
The below example define "How to declare static variable in Php".
<?php
function staticTest()
{
static $i = 0;
echo $i;
$i++;
}
?>
The below example define "How to declare static variable in java".
public class stacticTest {
public static int count = 0;
public stacticTest() {
count++;
}
public static void main(String[] args) {
stacticTest obj1 = new stacticTest();
System.out.println("There are " + stacticTest.count + " stacticTest objects");
stacticTest obj2 = new stacticTest();
stacticTest obj3 = new stacticTest();
System.out.println("There are " + stacticTest.count + " stacticTest objects");
}
}
0 Comment(s)