Need some help about with Memcache.
I have created a class and want to store its objects in Memcache, finding trouble doing so please tell me where am I going wrong. Following is my code
// Class defined by me
class User
{
public $fname;
public $age;
/**
* @return unknown
*/
public function getfname() {
return $this->fname;
}
/**
* @return unknown
*/
public function getage() {
return $this->age;
}
/**
* @return unknown
*/
public function setfname() {
return $this->fname;
}
/**
* @return unknown
*/
public function setage() {
return $this->age;
}
}
//Code for Storing
<?php
$objMemcache = new Memcache();
$objMemcache->connect('127.0.0.1', 11211);
$obj = new User();
$obj->setfname('John');
$obj->setage(32);
$objMemcache->set('user1', $obj, false, 60);
$obj1 = new User();
$obj1->setfname('Doe');
$obj1->setage(23);
$objMemcache->set('user2', $obj1, false, 60);
var_dump($objMemcache->get('user1'));
?>
The problem is I am not able to make sure if the object is actually getting store in Memache coz when i try to retrieve it using the $objMemcache->get($key), the var_dump function prints nothing.
Please help.
Can you please explain the error in my code.
Thanks soulmerge, Frank and Kevin, the solution worked, just another doubt.
Making the class variables private worked fine but when i try to convert the class object into a JSON_STRING using json_encode() it gives me again an empty value, any suggestions on that
Your class is wrong, try this :
<?php
// use this to display errors
ini_set('error_reporting',E_ALL);
ini_set('display_errors',true);
// Class defined by me
class User
{
private $fname;
private $age;
/**
* @return string
*/
public function getfname() {
return $this->fname;
}
/**
* @return string
*/
public function getage() {
return $this->age;
}
/**
* @return void
*/
public function setfname($value) {
$this->fname = $value;
}
/**
* @return void
*/
public function setage($value) {
$this->age = $value;
}
}
$objMemcache = new Memcache();
$objMemcache->connect('127.0.0.1', 11211);
$obj = new User();
$obj->setfname('John');
$obj->setage(32);
$objMemcache->set('user1', $obj, false, 60);
$obj1 = new User();
$obj1->setfname('Doe');
$obj1->setage(23);
$objMemcache->set('user2', $obj1, false, 60);
var_dump($objMemcache->get('user1'));