tags:

views:

101

answers:

2

Excuse what is probably a really basic question but how do I achieve this.

$cache = New Mem;    
$cache->event($event)->delete;

Which is called via the function below. Without the ->delete, it works perfectly fine, but I just need to find a way to call the delete thus inside the function.

class Mem
{
    function event($event)
    {
     global $pdo;
     global $memcached;

     $key = md5(sha1($event) . sha1('events'));

     if ($this->delete)
     {
      return $memcached->delete($key);
     }
     else
     {
      return $memcached->get($key);
     }
    }
}

I hope that makes sense, sorry for my pseudo code basically for the delete part.

+1  A: 

Add a second parameter to the event function, which you check to figure out which operation you want to do.

class Mem
{
    function event($event, $shouldDelete)
    {
        global $pdo;
        global $memcached;

        $key = md5(sha1($event) . sha1('events'));

        if ($shouldDelete)
        {
                return $memcached->delete($key);
        }
        else
        {
                return $memcached->get($key);
        }
    }
}

$cache = new Mem;    
$cache->event($event, true); // true to delete, false to get
John Kugelman
+1  A: 

You're calling delete as if it were a method within your class - but you have no method called delete...Instead, you should evaluate the $event variable as I'm doing below and determine what action you'll take:

class Mem {

  private $pdo;
  private $memcached;

  public function event($event) {

    $key = md5(sha1($event) . sha1('events')); #from your original code

    switch ($event) {
      case "delete":
        #do delete stuff
        $this->memchached->delete($key);
        break;
      case "add":
        #do add stuff
        break;
    }
  }

}

Update: Following additional questions in comments...

class Event {

  private $key;

  public function __construct($key) {
    $this->key = $key;
  }

  public function delete($key) {
    # do delete
  }

}
Jonathan Sampson
How would I write a method in my class that would grab the "key" from the function in question? The swapping of the event variable isn't an option unfortunately.
James
I'm not 100% clear on what you're attempting, but I updated my answer with what I think you were trying to accomplish.
Jonathan Sampson
What you are doing is correct, but I am unable to use the event as a way to switch. I need it in a OOP fashion, like delete->event or event->function. If that makes sense?
James
$event must be a class then, if you wish to use it that way. May I ask why the event must be a class?
Jonathan Sampson
If you want $event to be a class, or an object, actually, you'll have to do something like I did at the end of my answer.
Jonathan Sampson
Thank you very much.
James