James
James

Reputation: 6008

PHP object question

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.

Upvotes: 0

Views: 166

Answers (2)

Sampson
Sampson

Reputation: 268344

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
  }

}

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 361605

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

Upvotes: 1

Related Questions