dtz33344 2012-10-01 12:49
浏览 20
已采纳

PHP __sleep()无法与APC一起使用

I have a problem with __sleep() function.

This is the code I'm using. If I remove __sleep() function from class then everything works as expected.

class Test {
    private $name;

    function setName($value){
        $this->name = $value;
    }

    function getName(){
        return $this->name;
    }

    /* Works good without this function */
    public function __sleep() {
        echo 'Sleep';
    }
}

$obj = new Test;
$obj->setName('Juris');

apc_store('test', $obj);

$objAPC = apc_fetch('test');

// Output = Juris    
echo $obj->getName();
// No output and "Call to a member function getName() on a non-object" if __sleep() function is in class. Otherwise output = Juris
echo $objAPC->getName();

Why is this code not working? Are there any limitations with using APC and __sleep()?

PHP version: 5.3.14

APC version: 3.1.10

UPDATE from answer:

This will work if I change __sleep() function to this

public function __sleep() {
    return array('name');
}
  • 写回答

1条回答 默认 最新

  • douzhan8652 2012-10-01 12:52
    关注

    The magic __sleep() method must return an array of property names that should be serialized, you're returns nothing.

    Quoting from the manual (my emphasis):

    It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退