views:

281

answers:

4

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?

I am running PHP5

+6  A: 

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.

grepsedawk
Just to elaborate on PEAR -- you don't have to submit your code to PEAR, but you can still create your own PEAR-style packages. You could even setup a private channel for distribution.
Till
Yes this is what I mean by local PEAR repository, and gave a link explaining how to do just that.
grepsedawk
+2  A: 

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/node/view/id/1021

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.

dancavallaro
I'm going to give this a try... if it turns out good, and nothing better comes along Ill accept it.
Unkwntech
A: 

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.

jmucchiello
Well honestly it get annoying to maintain right now there is a file in the default include class that contains 40+ includes to get all the functions and it's getting a bit old if they were in the core I also would not have to remember to include the include file.
Unkwntech
Still, if you have a single file that loads them all, how hard is it to remember to call require_once "thefile.php"; in your script? Includes are just a way of life with PHP.
jmucchiello
A: 

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);
OIS
Namespaces are for php 5.3, which is a preview of php 6. I think the author of the question had php 5.2 in mind.
Tom
Yes. My answer is mostly about 5.2, but with comment about namespaces in 5.3. Basically saying, if he wants to use namespaces, he'll still have to put it in a class if he wants to use autoload anyway.
OIS