Adding functions to PHP core

前端 未结 5 800
礼貌的吻别
礼貌的吻别 2021-01-22 17:56

I have several functions that I wrote and I use regularly on my servers, is there a way I can add them to the core so I don\'t have to include them from external files?

5条回答
  •  甜味超标
    2021-01-22 18:46

    If you got autoload, you can move the functions as static methods of a class like My_Functions.

    Or for dividing it into more files you can use My_Functions_Math. Then you will only need to load the functions you need. And with autoload you don't have to worry about including files.

    You cant autoload namespace functions, so if you want to use autoload the functions have to be static methods in a class. But you can use namespace to make it easier to fx replace the class in the future and/or shorten the long class name. Example:

    use My\Functions\Math as Math;
    Math::calcThis($i);
    

提交回复
热议问题