PHP包含/需要内部函数 [英] PHP include/require inside functions

查看:112
本文介绍了PHP包含/需要内部函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有相当大的函数,并且每次加载页面时加载它们,最好编写函数foo(){include(... / file_with_function's_code);返回; } 最大限度地减少函数脚本的大小?或者它并不重要,因为当一个函数被加载(但不被执行)时,即使它被加载到内容中也会加载该内容?谢谢。



(编辑:我的问题不在于是否可能)

解决方案

虽然@Luceos的答案在技术上是正确的(最好是正确的),但它并不回答你问的问题,即做得更好,还是包括发生而不管函数调用?



我以最基本的方式测试了这个(OP,为什么你不?):

 <?php 
echoTesting ...;

函数doThing(){
包含nonExistantFile.php;
}
// doThing();
回显完成测试。

结果:

code> doThing(); 我找不到文件警告。



如果我注释掉 doThing (); ...没有错误!所以你可以通过这样做来节省文件加载时间。


Having functions that are quite big and they are loading every time the page is loaded, would be better to write function foo(){ include(.../file_with_function's_code); return; } to minimize the size of the functions script? Or it doesn't matter because when a function is loaded (but not executed) also is loaded the content even if it is into an include? Thank you.

(Edit: my question is not about if it's possible or not)

解决方案

While @Luceos answer is technically correct (the best kind of correct), it does not answer the question you asked, namely is doing this better, or do includes happen regardless of function calls?

I tested this in the most basic way (OP, why didn't you?):

<?php
echo "Testing...";

function doThing() {
    include nonExistantFile.php;
}
//doThing();
echo "Done testing.";

Results:

if I call doThing(); I get a file not found warning.

If I comment out doThing();... there is no error! So you can save file load time by doing this.

这篇关于PHP包含/需要内部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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