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.

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

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式