简体   繁体   中英

Why is __wakeup() called on my object when I session_start()?

I have a following problem:

I use a class with singleton pattern, i made the __wakeup() private for that reason. At no point is that object being stored in session. When i call session_start(), i get this error message:

Warning: Invalid callback MySingletonClass::__wakeup, cannot access private method MySingletonClass::__wakeup() in Session.php on line 32

line 32 of Session.php reads:

30 public function startSession() {
31     if (session_status() == PHP_SESSION_NONE) {
32         session_start();
33     }
34 }

I understand that objects stored in session are being serialized and unserialized, but MySingletonClass is never stored in session... In fact, the $_SESSION array is empty.

I was thinking that maybe i stored it in session by accident sometime and it sits there, but this persists even if i clear all $_SESSION data, destroy the session, delete session files on server, restart Apache and start new session...

What's even weirder is that i use several different classes that use exact same singleton pattern, have private __wakeup(), and they never raise this problem. Its just this one.

What is going on? Is my code so insane php got confused?

[edit] I figured something out:

This error happens if session_start() is called AFTER the MySingletonClass has been defined (so after the file containing its definition has been included/required) but does not if session_start() is moved to the very beginning of script execution, before any other files are included/required.

Does anyone have an explanation here? Having session_start() earlier is fine I guess, I would start it anyhow, but I would like to know what is happening here...

I resolved the issue - the problem was in my autoloading procedure. I think. Perhaps.

To be honest, i have no clue what was really wrong, but after i scrapped my autoloader class, and re-wrote it from scratch, the error message disappeared so i assume it was autoloader that was at fault.

Your guess is as good as mine.

Just had the same problem, with PHPUnit 5.7.27.

In my case, backupGlobals was set to "true". Setting it to false, in my phpunit.xml, fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM