PHP User-Created Memcache Objects

Need help with Memcache.

I created a class and want to store its objects in Memcache, having found trouble, so tell me where am I going wrong. Below 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 that I can't make sure the object actually gets storage in Memache coz, when I try to get it with $ objMemcache-> get ($ key) the var_dump function doesn't print anything.

Please, help.


Can you explain the error in my code.

Thanks soulmerge, Frank and Kevin, the solution worked, just another doubt.

Generally private class variables work fine, but when I try to convert the class object to JSON_STRING using json_encode () it gives an empty value again, any suggestions on this

+1


a source to share


1 answer


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'));

      

+1


a source







All Articles