I'm a AS3 coder and i do a bit of php and i am having a hard time doing a static class that can cache variables.
Here's what i have so far :
<?php
class Cache {
private static $obj;
public static function getInstance() {
if (is_null(self::$obj)){
$obj = new stdClass();
}
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
public static function set($key,$value){
self::$obj->$key = $value;
}
public static function get($key){
return self::$obj->$key;
}
}
?>
And i use the following to set my variable into an object of my static class :
<?php
include 'cache.php';
$cache = new Cache();
$cache->set("foo", "bar");
?>
And this is retrieve the variable
<?php
include 'cache.php';
$cache = new Cache();
$echo = $cache->get("foo");
echo $echo //doesn't echo anything
?>
What am i doing wrong ? Thank you
foo, you're storing the value in that particular instance. But you're creating a new instance when you want to retrieve it.getandsetmethods arestatic, so you need to use::instead of->when calling them.