|Adding functions to PHP core||#1|
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
posted date: 2008-12-21 20:08:00
|Re: Adding functions to PHP core||#3|
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.
posted date: 2008-12-21 20:11:00
|Re: Adding functions to PHP core||#4|
Can you be more specific. Do you mean functions written in PHP or functions written in C that you use from PHP (php extensions)?
posted date: 2008-12-21 20:14:00
|Re: Adding functions to PHP core||#5|
Yes written in PHP
posted date: 2008-12-21 20:32:00
|Re: Adding functions to PHP core||#6|
I'm going to give this a try... if it turns out good, and nothing better comes along Ill accept it.
posted date: 2008-12-21 20:36:00
|Re: Adding functions to PHP core||#7|
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.
posted date: 2008-12-21 20:46:00
|Re: Adding functions to PHP core||#8|
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.
posted date: 2008-12-21 20:53:00
|Re: Adding functions to PHP core||#9|
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);
posted date: 2008-12-21 23:07:00
|Re: Adding functions to PHP core||#10|
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.
posted date: 2008-12-21 23:38:00
|Re: Adding functions to PHP core||#11|
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.
posted date: 2008-12-22 04:37:00
|Re: Adding functions to PHP core||#12|
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.
posted date: 2008-12-22 04:55:00
|select page: « 1 2 »|