0

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.

2
  • What is delete? A variable? A method? A function? Commented Jun 27, 2009 at 16:46
  • I simply need to know how to pass into the function that we want to "delete" and thus execute memcached->delete instead of memcached->get Commented Jun 27, 2009 at 16:48

2 Answers 2

2

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
  }

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

5 Comments

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.
I'm not 100% clear on what you're attempting, but I updated my answer with what I think you were trying to accomplish.
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?
$event must be a class then, if you wish to use it that way. May I ask why the event must be a class?
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.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.