PHP include/require inside functions

后端 未结 4 1113
一个人的身影
一个人的身影 2020-12-18 21:24

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

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 21:52

    Or, as a good alternative, encapsulate your functions in classes, and take the benefit of __autoload :

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

    Encapsulate myBigFunction() in a class

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

    save it as myBigFunction.php

    When you call the function as static method on the class :

    myBigFunction::run()
    

    __autoload will load the file, but not before that.

提交回复
热议问题