0

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

3
  • Your issue here is that you're trying to construct a singleton, but you cannot instantiate it. When you set foo, you're storing the value in that particular instance. But you're creating a new instance when you want to retrieve it. Commented Feb 9, 2012 at 23:24
  • @fuzzyDunlop , ok i see so i went $cache = Cache::getInstance(); $cache->set("foo", "bar"); to set and $cache = Cache::getInstance(); echo $cache->get("foo"); still doesn't work :( Commented Feb 9, 2012 at 23:28
  • In this code, your get and set methods are static, so you need to use :: instead of -> when calling them. Commented Feb 9, 2012 at 23:41

2 Answers 2

1

I've adapted @prodigitalson's code above to get something rudimentary that works (and has much room for improvement):

class VarCache {
  protected static $instance;
  protected static $data = array();

  protected function __construct() {}

  public static function getInstance() {
     if(!self::$instance) {
       self:$instance = new self();
     }

     return self::$instance;
  }

  public function get($key) {
     self::getInstance();

     return isset(self::$data[$key]) ? self::$data[$key] : null;
  }

  public function set($key, $value) {
     self::getInstance();
     self::$data[$key] = $value;
  }
}

Usage

VarCache::set('foo', 'bar');
echo VarCache::get('foo');
// 'bar'

You'll want this class to be available everywhere you need it, and if you want it to persist between requests, I'd consider using Memcached or something similar, which will give you everything you need.

You could alternatively use some SPL functions, like ArrayObject, if you wanted to be clever :)

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed Memcache (without the D - I'm on WAMP) was what i really needed. I just installed it and it rules. For people like me on WAMP who want to install it as well, check this out : shikii.net/blog/installing-memcached-for-php-5-3-on-windows-7
@mrlee what kind of "room for improvement" do you think of ?
1

Try this:

class VarCache {
  protected $instance;
  protected $data = array();
  protected __construct() {}

  public static function getInstance()
  {
     if(!self::$instance) {
       self:$instance = new self();
     }

     return self::$instance;
  }

  public function __get($key) {
     return isset($this->data[$key]) ? $this->data[$key] : null;
  }

  public function __set($key, $value) {
     $this->data[$key] = $value;
  }
}

// usage

VarCache::getInstance()->theKey = 'somevalue';
echo VarCache::getInstance()->theKey;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.