For including files in an isolated context, but with injected variables, and potentially extracted variables.
composer require grithin/file-include
use \Grithin\FileInclude;
# inject `bob` into the file context, so file code can access $bob
FileInclude::include('file.php', ['bob'=>$bob]);
# inject the global $bob into the file
global $bob;
FileInclude::require_once('file2.php', null, ['globals'=>['bob']]);
Extraction will adjust the return value. Whereas, normally, the return value is the return of the file, when variables are being extracted, the return changes into an array, where the normal return can be found in ['_return'=>$x]
file.php
$bob = 'bob';
return 123;
$result = FileInclude::include_once('file.php', null, ['extract'=>['bob']]);
/*>
[
'_return'=>123,
'bob'=>'bob'
]
*/
This is a rewrite and selection from \Grithin\Files in phpbase