简体   繁体   English

PHP包含/ require内部函数

[英]PHP include/require inside functions

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; } 拥有相当大的函数并且每次加载页面时都会加载它们,最好编写function foo(){ include(.../file_with_function's_code); return; } function foo(){ include(.../file_with_function's_code); return; } function foo(){ include(.../file_with_function's_code); return; } to minimize the size of the functions script? function foo(){ include(.../file_with_function's_code); return; }最小化函数脚本的大小? 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? 虽然@Luceos答案在技术上是正确的(最好的正确),但它没有回答你问的问题,即更好地做到这一点,还是包括无论函数调用发生的事情?

I tested this in the most basic way (OP, why didn't you?): 我用最基本的方式测试了这个(OP,为什么不是你?):

<?php
echo "Testing...";

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

Results: 结果:

if I call doThing(); 如果我打电话给doThing(); I get a file not found warning. 我收到一个未找到文件的警告。

If I comment out doThing(); 如果我注释掉doThing(); ... there is no error! ......没有错误! So you can save file load time by doing this. 这样你就可以节省文件加载时间。

Or, as a good alternative, encapsulate your functions in classes, and take the benefit of __autoload : 或者,作为一个很好的替代方案,将您的函数封装在类中,并从__autoload获益:

function __autoload($class_name) {
    include $class_name . '.php';
}

Encapsulate myBigFunction() in a class myBigFunction()封装在一个类中

class myBigFunction {
  public static function run() {
    //the old code goes here
  }
}

save it as myBigFunction.php 将其保存为myBigFunction.php

When you call the function as static method on the class : 当您在类上调用该函数作为静态方法时:

myBigFunction::run()

__autoload will load the file, but not before that. __autoload将加载文件,但不会在此之前加载。

Yes that's possible; 是的,这是可能的; see http://www.php.net/manual/en/function.include.php http://www.php.net/manual/en/function.include.php

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. 如果include发生在调用文件中的函数内部,则调用文件中包含的所有代码将表现为就好像已在该函数内定义的那样。 So, it will follow the variable scope of that function. 因此,它将遵循该函数的可变范围。

Question is, why not add the surrounding function definition to that included file. 问题是,为什么不将周围的函数定义添加到包含的文件中。 I think the only viable reason to include within a function is to split code within that function into bits. 我认为在函数中包含的唯一可行理由是将该函数中的代码分成位。

Both Luceos ' and Albatrosz could be misread, so I felt that I should clarify these. Luceos '和Albatrosz都可能被误读,所以我觉得我应该澄清这些。

The include set of directives generate a runtime ZEND_INCLUDE_OR_EVAL operation which calls the Zend compiler to compile the referenced file. include指令集生成运行时 ZEND_INCLUDE_OR_EVAL操作,该操作调用Zend编译器来编译引用的文件。 So in general you should not embed include statements in a function, as: 所以通常你不应该在函数中嵌入include语句,如:

  • The include will be executed every time that code path is taken when the function is called. 每次在调用函数时获取代码路径时,都会执行include。 Compiling the same bit of code 100s of times is a bad idea. 编译相同的代码100次是一个坏主意。

  • If the code contains elements of global scope (eg function or class declarations) then executing that declaration even twice will cause compiler errors. 如果代码包含全局范围的元素(例如函数或类声明),则执行该声明甚至两次将导致编译器错误。

So don't unless you know what you are doing. 除非你知道自己在做什么,否则要这样做。 Use techniques such are those described by Albatrosz. 使用Albatrosz描述的技术。 Incidentally his __autoload() function is just the sort of example of an exception where this is valid to do. 顺便提一下,他的__autoload()函数只是一个例子,它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM