Adding new functions to environment?
Moderator: General Moderators
-
ben.artiss
- Forum Contributor
- Posts: 116
- Joined: Fri Jan 23, 2009 3:04 pm
Adding new functions to environment?
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
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
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Adding new functions to environment?
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)
-
ben.artiss
- Forum Contributor
- Posts: 116
- Joined: Fri Jan 23, 2009 3:04 pm
Re: Adding new functions to environment?
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?
If you've got a file like this:
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.
Code: Select all
<?PHP
include 'file/with/makeSafe.php';
include 'other/file.php';
include 'yet/anotherfile.php'
?>Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
ben.artiss
- Forum Contributor
- Posts: 116
- Joined: Fri Jan 23, 2009 3:04 pm
Re: Adding new functions to environment?
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
/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?
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
ben.artiss
- Forum Contributor
- Posts: 116
- Joined: Fri Jan 23, 2009 3:04 pm
Re: Adding new functions to environment?
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?
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).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
ben.artiss
- Forum Contributor
- Posts: 116
- Joined: Fri Jan 23, 2009 3:04 pm
Re: Adding new functions to environment?
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?
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 
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
ben.artiss
- Forum Contributor
- Posts: 116
- Joined: Fri Jan 23, 2009 3:04 pm
Re: Adding new functions to environment?
That you're right my good man
far too lazy to learn C!