Forum Moderators: coopster

Message Too Old, No Replies

Functions

Where are user defined functions stored?

         

panic

8:43 pm on Oct 6, 2003 (gmt 0)

10+ Year Member



I have a ton of PHP functions that I've made on seperate PHP files. Is there anywhere that I can keep user-defined functions so that I don't have to define them in EVERY one of my PHP files?

-panic

jatar_k

1:06 am on Oct 7, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hey panic, you lost me a bit. Did you jsut want to centralize your functions for use in every script?

If so, you could put them all into a single file and and include it. (not sure if that is what you are asking though)

RobinC

1:36 am on Oct 7, 2003 (gmt 0)

10+ Year Member



Looks like he just wants to use one of - include, include_once, require or require_once - read the php help for those and if it's not right, then get back to us ;-)

panic

4:21 pm on Oct 7, 2003 (gmt 0)

10+ Year Member



What I want to do is to be able to use those functions without having to require/include/etc another PHP file. I want to be able to call it directly from PHP, just as I would any other function.

Maybe it'd be done using a module, or...?

-panic

lorax

4:28 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't believe you can do that with PHP. PHP is a script and in order to execute any functions the PHP engine needs to read them so you need to include them somehow. Unless someone has a clever way of getting around the obvious I think you're stuck.

The only option left to you is to write them as objects that can be called anytime.

panic

4:43 pm on Oct 7, 2003 (gmt 0)

10+ Year Member



lorax, that's where you're wrong. I just got the answer I was looking for.

You copy your PHP file with all your functions to /usr/local/lib/php/ (we'll call it customfunctions.php). Then, in your php.ini, search for auto_prepend_file, and set it to :

auto_prepend_file = customfunctions.php

(Thanks to my buddy Justin/PyroX)

-panic

ergophobe

5:06 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Lorax isn't exactly wrong depending on how one interprets your question. All you're really doing with your auto-prepend is using a different method to include/require that file.

As the manual sez


auto_prepend_file.

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the include() function, so include_path is used.

You say you want to


use those functions without having to require/include/etc another PHP file

which you aren't really achieving. What you are doing is saving yourself the trouble of putting a require/include statement in every page header, but it isn't quite the same as achieving what you state in your post. That may seem like stupid nitpicking (okay, so it probably IS stupid nitpicking), but I only mean to say that your server overhead should be very similar whether you autoprepend or do it manually, so it will achieve half of your result (less coding) but not the other half implied by your question (reduced overhead by not including functions unless you need them).

Tom

lorax

5:59 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> lorax, that's where you're wrong.

LOL - well it's not the first time. I forgot about auto-prepend. But as ergophobe points out, it is simply an automated include.

panic

6:01 pm on Oct 7, 2003 (gmt 0)

10+ Year Member



What you are doing is saving yourself the trouble of putting a require/include statement in every page header

That's the whole idea.

but it isn't quite the same as achieving what you state in your post.

Actually, it is.

...reduced overhead by not including functions unless you need them

That's not an issue since it's only for internal use, and I'll be the only one using it. And besides, an extra kilobyte wont hurt you at all if you're running everything localhost.

-panic

gg

ergophobe

7:06 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My only point was that there were two ways of interpreting it, and I was just trying to give lorax the benefit of the doubt... even if it turns out that he was right but for the wrong reason :-)

Interpreting your post to the letter (according to what it said), lorax was right. Interpreting it according to your intent, lorax was wrong, but your intent wasn't clear.

Doesn't really matter as long as you're happy with the solution though.

Tom