|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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" } }
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 03:00:02 2026 UTC |
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.