php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68138 spl_autoload_register fails to load object from session after session_start()
Submitted: 2014-10-02 20:42 UTC Modified: 2014-10-02 20:59 UTC
From: michal dot rudnicki at epsi dot pl Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.5.17 OS: Windows/Linux
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
12 - 11 = ?
Subscribe to this entry?

 
 [2014-10-02 20:42 UTC] 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" } } 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-02 20:59 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-10-02 20:59 UTC] 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 21:38 UTC] michal dot rudnicki at epsi dot pl
Thank you. This clarifies. I didn't realize it had something to do with symbols creation at parse time vs registration at run time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC