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 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决