grithin/file-include

Include files in isolated context with options

0.1 2021-07-20 20:15 UTC

This package is auto-updated.

Last update: 2024-11-21 03:14:49 UTC


README

For including files in an isolated context, but with injected variables, and potentially extracted variables.

composer require grithin/file-include

Use

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

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'
]
*/

Notes

This is a rewrite and selection from \Grithin\Files in phpbase