-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird behaviour when a file is autoloaded in assignment of a constant #10232
Comments
I don't think this could ever have worked, unless you set So, have you set |
I see I made a tiny mistake in the name of the file "test/required.php" just fixed that. But yes test.php is in the location as indicated one level above the files included. And constant.php and required.php are in the same directory. Reading through the documentation and seems that you are right it seems that requiring like this is not supposed to work, but it still does mostly work and did completely work for the previous versions. |
Retested this bug on PHP 8.2.1. with FPM/FastCGI and Oracle Linux 8 same result |
I think I have confirmed the issue on PHP 8.2.1 CLI. From the document of include():
And the issue is about a misidentification of the script's directory. mkdir empty first second
cat <<'END' > main.php
<?php
chdir(__DIR__ . '/empty');
set_include_path(getcwd());
spl_autoload_register(function () {
require_once __DIR__ . '/second/SecondClass.php';
}, true);
$case = (int)$argv[1];
require_once __DIR__ . '/first/FirstClass.php';
new FirstClass();
END
cat <<'END' > first/FirstClass.php
<?php
if ($case == 1): // fails
class FirstClass { public const VALUE = SecondClass::VALUE; }
elseif ($case == 2): // fails
class FirstClass { public $value = SecondClass::VALUE; }
elseif ($case == 3):
class FirstClass { public function __construct() { SecondClass::VALUE; } }
elseif ($case == 4):
class FirstClass {}
SecondClass::VALUE;
endif;
END
cat <<'END' > second/SecondClass.php
<?php
require_once 'required.php'; // should load the file in the same directory
class SecondClass { public const VALUE = 1; }
END
cat <<'END' > required.php
<?php echo basename(__DIR__), "\n";
END
cp required.php first/
cp required.php second/
php82 main.php 1 # first
php82 main.php 2 # first
php82 main.php 3 # second
php82 main.php 4 # second
php81 main.php 1 # second
php81 main.php 2 # second
php81 main.php 3 # second
php81 main.php 4 # second main.php explicitly loads FirstClass.php, which loads SecondClass.php by autoloading, and then SecondClass.php loads required.php. |
Since php-src/Zend/zend_execute_API.c Lines 609 to 614 in 479b47c
|
Hi, |
Description
The following files:
test.php
test/constant.php
test/required.php
<?php
Resulted in this output:
But I expected this output instead:
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 fineIn 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
The text was updated successfully, but these errors were encountered: