grithin / file-include
在隔离的上下文中包含文件,并带有选项
0.1
2021-07-20 20:15 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- grithin/phpbase: ^5.0
This package is auto-updated.
Last update: 2024-09-21 02:54:26 UTC
README
用于在隔离的上下文中包含文件,但带有注入的变量,以及可能提取的变量。
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']]);
提取
提取将调整返回值。而通常,返回值是文件的返回值,当变量被提取时,返回值变为一个数组,其中正常返回可以在 ['_return'=>$x]
中找到。
file.php
$bob = 'bob'; return 123;
$result = FileInclude::include_once('file.php', null, ['extract'=>['bob']]); /*> [ '_return'=>123, 'bob'=>'bob' ] */
备注
这是从 phpbase 中的 \Grithin\Files 重新编写和选择的内容。