Skip to content

Weird behaviour when a file is autoloaded in assignment of a constant  #10232

Closed
@fasterforward

Description

@fasterforward

Description

The following files:

test.php

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

class Loader
{
    public function __construct()
    {
        spl_autoload_register([$this, 'include'], true);
    }
    public function include()
    {
        require 'test/constant.php';
    }
}

new Loader();
class B
{
    public const A = constant::TEST;
}

new B();
echo 'here';

test/constant.php

<?php

declare(strict_types=1);
require_once 'required.php';

class constant
{
    public const TEST = 'test';
}

test/required.php

<?php

Resulted in this output:

Warning: require_once(required.php): Failed to open stream: No such file or directory in /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php on line 21 Call Stack: 0.0183 365472 1. {main}() /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php:0 0.1607 397352 2. Loader->include('constant') /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php:24 0.1611 397752 3. require_once('/mnt/fasterforward/web01/elements/src/httpdocs/test/test.php') /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php:11 Fatal error: Uncaught Error: Failed opening required 'required.php' (include_path='.:/usr/share/pear:/usr/share/php:/usr/share/pear:/usr/share/php') in /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php on line 21 Error: Failed opening required 'required.php' (include_path='.:/usr/share/pear:/usr/share/php:/usr/share/pear:/usr/share/php') in /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php on line 21 Call Stack: 0.0183 365472 1. {main}() /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php:0 0.1607 397352 2. Loader->include('constant') /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php:24 0.1611 397752 3. require_once('/mnt/fasterforward/web01/elements/src/httpdocs/test/test.php') /mnt/fasterforward/web01/elements/src/httpdocs/test/test.php:11 

But I expected this output instead:

here

For some reason in when a file is autoloaded in assignment of a constant, the requires there do not get checked based on the location of that file.
When I put the autoloader outside of a class, require the file directly or specify constant as a type instead it works fine
In PHP 8.1.8 and PHP 7.4.30 with very simular php.ini's it works fine and the file gets required properly.
I thought it might be opcache but when I disable it the same problem happens.

PHP Version

PHP 8.2

Operating System

Red Hat Enterprise Linux release 8.7

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions