Bug #68138 [Com]: spl_autoload_register fails to load object from session after session_start()
| From: | michal dot rudnicki at epsi dot pl | Date: | Thu, 02 Oct 2014 21:38:24 +0000 |
| Subject: | Bug #68138 [Com]: spl_autoload_register fails to load object from session after session_start() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187809@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68138&edit=1
ID: 68138
Comment by: michal dot rudnicki at epsi dot pl
Reported by: michal dot rudnicki at epsi dot pl
Summary: spl_autoload_register fails to load object from
session after session_start()
Status: Not a bug
Type: Bug
Package: Session related
Operating System: Windows/Linux
PHP Version: 5.5.17
Block user comment: N
Private report: N
New Comment:
Thank you. This clarifies. I didn't realize it had something to do with symbols creation at
parse time vs registration at run time.
Previous Comments:
------------------------------------------------------------------------
[2014-10-02 20:59:35] requinix@php.net
Regular functions are identified, so to speak, when the containing file is first loaded and parsed*.
PHP knows about __autoload immediately so when the session begins, even though execution hasn't
actually reached the "function __autoload($className) {" line yet, PHP can run the
autoloader.
Not the case with spl_autoload_register() because it's an actual statement that has to be
executed to set up the autoloader. *Do not do this* but if you kept __autoload as a function and did
spl_autoload_register("__autoload");
then everything would work - but it would do so through the older __autoload convention and not
through the newer SPL method.
You must do session_start() after installing the autoloader.
* if at the top-most scope in the file, ie. not within conditionals or loops or other structures.
------------------------------------------------------------------------
[2014-10-02 20:42:33] michal dot rudnicki at epsi dot pl
Description:
------------
Hi All,
Problem:
When loading classes with __autoload, restoring objects from session works regardless whether
session_start() was called before or after __autoload was declared.
When loading classes with spl_autoload_register, restoring objects from session only works when
session_start() called after registering loader, and returns __PHP_Incomplete_Class object when
called before registering loader.
PHP and OS versions:
- 5.5.1 on Windows 2008 Server 64 bit
- 5.5.9 on Ubuntu Linux 14.04 64 bit
Preparation:
Create file Foo.php with empty declaration of class Foo.
Test script:
---------------
<?php
session_start();
spl_autoload_register(function ($className) { include "{$className}.php"; });
isset($_SESSION["foo"]) or $_SESSION["foo"] = new Foo();
var_dump($_SESSION);
Expected result:
----------------
array(1) { ["foo"]=> object(Foo)#2 (0) { } }
Actual result:
--------------
array(1) { ["foo"]=> object(__PHP_Incomplete_Class)#1 (1) {
["__PHP_Incomplete_Class_Name"]=> string(3) "Foo" } }
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68138&edit=1