php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76636 autoloading no longer catches and rethrows exceptions
Submitted: 2018-07-17 10:16 UTC Modified: 2018-12-09 05:19 UTC
From: sugerliedear at gmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 7.3.0 OS: centos65
Private report: No CVE-ID: None
 [2018-07-17 10:16 UTC] sugerliedear at gmail dot com
Description:
------------
When i invoke spl_autoload_register two times, E_PARSE error not throw in the spl_autoload_register in the first time. 

Test script:
---------------
https://github.com/khadgarmage/hello-world/blob/master/php72-bug1.md

Expected result:
----------------
root@/mnt/htdocs $ php72 t.php
object(ParseError)#3 (7) {
  ["message":protected]=>
  string(95) "syntax error, unexpected 'fdsaf' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)"
  ["string":"Error":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(17) "/data2/mytest.php"
  ["line":protected]=>
  int(3)
  ["trace":"Error":private]=>
  array(2) {
    [0]=>
    array(2) {
      ["function"]=>
      string(9) "{closure}"
      ["args"]=>
      array(1) {
        [0]=>
        string(6) "mytest"
      }
    }
    [1]=>
    array(4) {
      ["file"]=>
      string(12) "/data2/t.php"
      ["line"]=>
      int(23)
      ["function"]=>
      string(17) "spl_autoload_call"
      ["args"]=>
      array(1) {
        [0]=>
        string(6) "mytest"
      }
    }
  }
  ["previous":"Error":private]=>
  NULL
}
PHP Warning:  require_once(namespace\mytest.php): failed to open stream: No such file or directory in /data2/t.php on line 19
PHP Fatal error:  require_once(): Failed opening required 'namespace\mytest.php' (include_path='.:/opt/remi/php72/root/usr/share/pear:/opt/remi/php72/root/usr/share/php:/usr/share/pear:/usr/share/php') in /data2/t.php on line 19

Actual result:
--------------
root@/mnt/htdocs $ php72 t.php
PHP Warning:  require_once(namespace\mytest.php): failed to open stream: No such file or directory in /data2/t.php on line 15
PHP Fatal error:  require_once(): Failed opening required 'namespace\mytest.php' (include_path='.:/opt/remi/php72/root/usr/share/pear:/opt/remi/php72/root/usr/share/php:/usr/share/pear:/usr/share/php') in /data2/t.php on line 15

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-17 11:45 UTC] requinix@php.net
-Summary: E_PARSE error not throw in the spl_autoload_register. +Summary: autoloading catches and rethrows exceptions -Status: Open +Status: Suspended -Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: Scripting Engine problem -Assigned To: +Assigned To: requinix
 [2018-07-17 11:45 UTC] requinix@php.net
Your first autoloader will throw an exception, but PHP catches it and continues autoloading. When autoloading completes the exception would have been rethrown, however your second autoloader fails with a fatal *error* so PHP exits before that happens.

...but that only applies to PHP 7.2 and earlier. See bug #74372.

spl_autoload_register should get a changelog entry about 7.3 so I'm suspending this until that's released.
 [2018-07-17 11:47 UTC] requinix@php.net
Oh, and I forgot:

Do not blindly require() files in an autoloader. Check that they exist first with is_file().
 [2018-12-09 05:19 UTC] requinix@php.net
-Summary: autoloading catches and rethrows exceptions +Summary: autoloading no longer catches and rethrows exceptions -Status: Suspended +Status: Verified -PHP Version: 7.2.7 +PHP Version: 7.3.0 -Assigned To: requinix +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 09:01:31 2024 UTC