PHP“取消包含";或从内存中删除? [英] PHP "Uninclude" or remove from memory?

查看:87
本文介绍了PHP“取消包含";或从内存中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我正在为该工具使用PHPUnit,但我正在构建一个测试工具以在很难进行单元测试的遗留PHP代码目录上执行.

I am building a testing tool to execute on directories of legacy PHP code that is extremely difficult to unit test, although I am using PHPUnit for this tool.

关于PHPUnit的一件很酷的事情是,它将PHP通知,警告和错误冒泡为一个异常.利用这些知识,我构建了一个工具,可以从try/catch块中的目录中递归地include()文件并记录所有异常.

One of the cool things about PHPUnit is that it will bubble up PHP notices, warnings, and errors into an exception. Using that bit of knowledge I've built a tool to recursively include() files from a directory in a try/catch block and log any exceptions.

问题是,如果我包含成百上千个文件,这可能很快成为内存消耗和崩溃.我已经考虑过使用它,但是我不确定这是否可以从内存中清除"它:

The problem is, this can quickly become a memory hog and crash if I include hundreds of files. I've considered using this but I'm not sure if this "clears" it from memory:

// Include file into buffer
ob_start();

include($file);

// Clear file from buffer
ob_clean();

处理这种性质并适当管理内存/资源的最佳方法是什么?

What is the best way to handle something of this nature and manage memory/resources appropriately?

此外,这是我正在开发的内部工具,用于管理包含 unestable (在时间限制的意义上)旧程序代码的数千个文件.

Also, this is an internal tool that I am developing to help manage the thousands of files containing untestable (in a time constraint sense) legacy procedural code.

这是一个想法吗?

// Enable garbage collector (in case this helps?)
gc_enable();

// Get procedural php and execute it
$fileData = file_get_contents($file);
eval($fileData);

// Clear variable
$fileData = null;

推荐答案

如果您创建一个单独的CLI脚本(测试器"),该脚本一次包含一个文件并记录异常,并具有主脚本,该怎么办? 迭代器")遍历/递归到您需要测试的脚本目录中,并通过外壳针对每个文件调用测试器脚本?

What if you create a separate CLI script (the "tester") that will include one file at a time and log the exceptions, and have your main script (the "iterator") iterate through / recurse into the directories of scripts you need to test and invoke the tester script via the shell for each file?

这篇关于PHP“取消包含";或从内存中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆