Skip to content

Arrow function vs require_once #18943

Open
@zboszor

Description

@zboszor

Description

The following code:

retfunc.php

<?php
return array('a', 'b');

test-loadfile.php

<?php
$path = 'retfunc.php';
$i = 1;
$data = (fn () => require_once $path)();
while (is_array($data)) {
    $i++;
    $data = (fn () => require_once $path)();
    if ($i > 100)
        break;
}

if (!is_array($data)) {
    echo 'not an array in round ' . $i . PHP_EOL;
    var_dump($data);
} else
    echo 'success' . PHP_EOL;

Resulted in this output:

$ php test-loadfile.php
not an array in round 2
bool(true)

But I expected this output instead:

$ php test-loadfile.php
success

This is a minimal example that mimics Laravel's laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php that loads configuration files.

Maybe it's an undocumented corner case, but require_once should return the file source the second and subsequent times without actually loading the file again, so the arrow function would produce the array value.

The example code works if both arrow functions use require.

PHP Version

PHP 8.4.8 (cli) (built: Jun  3 2025 16:29:26) (NTS gcc x86_64)
Copyright (c) The PHP Group
Built by Fedora Project
Zend Engine v4.4.8, Copyright (c) Zend Technologies

Operating System

Fedora 42

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