Adding functions to PHP core

前端 未结 5 753
礼貌的吻别
礼貌的吻别 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:38

    I've done this before.. it's a fairly involved process, but not too bad. This article at zend.com should tell you everything you need to know:

    http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

    edit: I should add that there are better ways to achieve the essence of what you're trying to do. Remember that doing this will further clutter up PHP's (already very cluttered) namespace. You're probably better off just making a global include file that has all of your most commonly used functions that you include wherever you need it.

    edit2: Upon rereading your original question, you said you don't want to do that, but I still think it's probably the best way. But best of luck to you with the extension route.

    0 讨论(0)
  • 2021-01-22 18:45

    If you want your function to always be available, without including it, do this:

    Create your function in an php file.

    In your php.ini search for the option "auto_prepend_file" and add your php file to that line, like this: auto_prepend_file = "/path/to/my_superglobal_function.php"

    Or if you write without a path like this: auto_prepend_file = "my_superglobal_function.php" It will look in your include_path in php.ini to find the file.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-22 18:50

    You could add your libraries as a PEAR extension. Then you could add it to your local PEAR repository. Pear is added to the default include path in php.ini. Then you can just use "pear install myextension" on your machines.

    If these are C functions you interface with in php (php extensions) then you can do something similar with PECL.

    0 讨论(0)
  • 2021-01-22 18:50

    Why exactly is it so hard to include the files where you need them?

    I suppose the auto_prepend_file PHP.ini directive could work. But it's not really recommended.

    0 讨论(0)
提交回复
热议问题