Page 1 of 1
Adding new functions to environment?
Posted: Tue Mar 03, 2009 9:29 am
by ben.artiss
Hi,
I have a set of functions I usually include in all of my PHP projects to make life a bit easier and to speed up the process, but it hit me I might be able to make these 'temporarily permanent' - but I'm not too sure how safe it is to play with the $_ENV or whether it's even possible to add functions to the environment or on a global scope.
Say I wanted to make a custom function (e.g. makeSafe()) available to every file, is the best way just to include that function in every file or can I load it once - probably based on some condition - and have it available to every other file thereafter like normal (built-in) functions? I have a feeling this may be more to do with server configuration or something but I just don't know. The method I'm using at the moment is fine but it's just annoying when you're using relative paths for things.
Apologies if that's a bit vague!
Thanks, Ben
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 11:18 am
by Mark Baker
Well you could always write it as a PHP module, or set it as an auto_prepend_file via php.ini (which would automatically include it before any PHP script was executed)
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 11:27 am
by ben.artiss
Great stuff thanks for the reply. Do servers allow you to use your own modules? Sorry if that's a stupid question I've never used modules before!
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 11:34 am
by pickle
If you've got a file like this:
Code: Select all
<?PHP
include 'file/with/makeSafe.php';
include 'other/file.php';
include 'yet/anotherfile.php'
?>
And makeSafe.php has the makeSafe() function, that function will also be available to file.php and anotherfile.php . You don't need to include makeSafe.php in file.php and anotherfile.php.
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 11:54 am
by ben.artiss
I know what you're saying but it's not quite what I'm asking (sorry if I'm not clear!). What I was thinking was, say I have the following files:
/lib/globals.php (contains the functions I want to be available to every file)
/index.php
/about.php
/contact.php
Instead of include('lib/globals.php') in each of the other files, would there be a way of declaring the functions once and making them globally available without having to include the same file every time? Or does this take us back to modules?
Thanks for the help
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 12:07 pm
by pickle
I think it would be much simpler to use the auto_prepend setting than to create your own modules (that have to be created in C/C++ I think). If your server allows you to mess with the php.ini file - great!. If not, you can use
ini_set() to do it - though if you do that you might as well just
include() the file.
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 12:10 pm
by ben.artiss
Thanks for clearing that up, like I said there's nothing wrong with what I'm doing now with the include() method, I was just trying to make it simpler! I'm even lazy at coding hehe that's bad stuff. Cheers guys.
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 12:15 pm
by pickle
It's not uncommon - in fact it's pretty much a standard - for each file in a project to have an include() at the top that brings in a bootstrap/setup/configuration file that includes any common functionality or values. I think this is a better option anyway, in case you have to move the code to a new server (one that may not have php.ini editing capabilities).
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 12:19 pm
by ben.artiss
It's the way I usually do things so I think I'll stick to it! I was just thinking of server load - u know, re-reading the same functions over and over on every page, but it's not a big deal. Thanks for your replies!
Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 12:33 pm
by pickle
Whether you use auto_prepend or not, the same file still needs to be read and parsed. The
only way you could reduce the clock cycles dedicated to enabling that function is to build a module. And rewriting your function in some C-variant doesn't sound like something a lazy person would do

Re: Adding new functions to environment?
Posted: Tue Mar 03, 2009 12:37 pm
by ben.artiss
That you're right my good man

far too lazy to learn C!