php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75369 spl_autoload_register prepend not working with nested require_once
Submitted: 2017-10-12 20:13 UTC Modified: 2017-10-13 20:41 UTC
From: tbollinger at ivesinc dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 7.1.10 OS:
Private report: No CVE-ID: None
 [2017-10-12 20:13 UTC] tbollinger at ivesinc dot com
Description:
------------
This bug requires three files to reproduce, since it is an issue with spl_autoload_register(), specifically the prepend argument, and require_once().
Removing the prepend argument displays the expected result. 
This was created using a base install of PHP 7.1.10.

Test script:
---------------
---- file1.php ----
<?php
function load() { 
    spl_autoload_register('foo'); 
}
function foo($class) {
    echo __FUNCTION__ . "\n";
    require_once('./file2.php');
}
load();
new FooBar();

---- file2.php ----
<?php
function bar($class) {
    echo __FUNCTION__ . "\n";
    require_once('./file3.php');
}
spl_autoload_register('bar', true, true);

---- file3.php ----
<?php
class FooBar {
    public function __construct() { echo __CLASS__ . "\n"; }
}



Expected result:
----------------
foo
bar
FooBar

Actual result:
--------------
foo
foo
PHP Fatal error:  Uncaught Error: Class 'FooBar' not found in /home/user/phptest/file1.php:5
Stack trace:
#0 {main}
  thrown in /home/user/phptest/file1.php on line 5

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-12 23:40 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2017-10-12 23:40 UTC] requinix@php.net
What is the bug, exactly? Why do you expect that behavior? If bar() is prepended during execution of foo() the autoload system isn't going to "go back" and use it, so when foo() ends FooBar won't be present. As you said, if you append instead of prepend then it works.
 [2017-10-13 17:45 UTC] tbollinger at ivesinc dot com
-Status: Feedback +Status: Open
 [2017-10-13 17:45 UTC] tbollinger at ivesinc dot com
Thank you, your comment makes sense. I mistakenly thought that the prepended bar() would be interpreted and the require_once() would be executed.

This can be closed as "not a bug". Thanks again for your time!
 [2017-10-13 17:46 UTC] tbollinger at ivesinc dot com
-Status: Open +Status: Closed
 [2017-10-13 17:46 UTC] tbollinger at ivesinc dot com
Closing ticket.
 [2017-10-13 20:41 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 10:01:29 2024 UTC