In Java, a static member maintains its value for all instances of the class. Can this be done in PHP? I remember hitting this issue a few years ago and my current test confirms that static member does not maintain its state. So I guess, in PHP a class is unloaded and all its state destroyed after each request.
index.php
include('cache.php');
$entityId=date('s');
$uri='page'.$entityId;
$cache = new Cache();
$cache->cacheUrl($uri, $entityId);
cache.php
class Cache {
private static $URL_CACHE;
public function cacheUrl($url, $entityId) {
echo '<br>caching '.$url.' as '.$entityId;
$URL_CACHE[$url]=$entityId;
echo '<br>Cache content:<br>';
foreach ($URL_CACHE as $key => $value) {
echo 'Key: '.$key.' Value: '.$value.'<br>';
}
}
}
Output (each time I get a single Key=>Value)
caching test33 as 33
Cache content:
Key: test33 Value: 33
I understand we do not have the concept of a JVM in PHP. Is there still a way to do this in a standard installation of PHP (typical VPS hosting service with a cPanel)?