Forum Moderators: coopster

Message Too Old, No Replies

custom functions

to be used across all domains

         

Jaunty Edward

1:51 am on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi,

I have made a few functions that I use on almost all my websites. I am thinking if there is a way to put these functions somewhere on the server so that they become like normal php functions(strpos,nl2br etc)

I think it should be possible, I will be thankful for any hint on that.

thanks
bye

[edited by: Jaunty_Edward at 1:53 am (utc) on Mar. 30, 2007]

eelixduppy

3:56 am on Mar 30, 2007 (gmt 0)



Without compiling your own version of PHP the only way I can think of is to use auto_prepend_file [us3.php.net] and include a script with your function declarations in there. That way each script will have them and you can use your functions whenever you need them. It will essentially have the same effect; not behind the scenes, though. :)

Good luck!

Jaunty Edward

5:14 am on Mar 30, 2007 (gmt 0)

10+ Year Member



wow,
so now I just have to write all my functions in siteone.com/phpfunctions.php

and I just have to add the following line in php.ini

auto_append_file('www.example.com/phpfunctions.php');

and it will start working on all domains on the server.

wondering if we will be able to refer phpfunctions.php file that way or will we have to put it in the php dir?

I will confirm this once I am done with it.

anyone has any other suggestions.

thanks
bye

[edited by: dreamcatcher at 7:25 am (utc) on Mar. 30, 2007]
[edit reason] Use example.com, thanks. [/edit]

henry0

12:01 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am also interested but will use them in two classes that I always use one is about regex and security steps the other one is about a few MySQL functions such as insert, update etc..
That should also work shouldn't it?

eelixduppy

12:42 pm on Mar 30, 2007 (gmt 0)




auto_append_file('www.example.com/phpfunctions.php');

The syntax in php.ini is


auto_prepend_file [i]string[/i]

Where string is the path to the file. Don't use a uri for the path because it will not be included as a php script, but rather the output from that script. Instead, refer to it using a relative path.

Henry, yes it will work with classes as well. It is the same thing as calling an include [php.net] at the beginning at every file, you just don't have to key it in every time.

joelgreen

2:46 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



From documentation:
automatically parsed before the main file

I assume it would only include file once, in the topmost file (referenced above as main file). Otherwise it would generate "cannot redeclare function" error sometimes.

henry0

5:10 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a good point!

cameraman

6:55 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How hard is it to write an extension - is that done in a compiled language with strict export guidelines and such?

henry0

7:17 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cameraman
Found Something [devzone.zend.com] at Zend.

Jaunty Edward

8:40 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



Hi,

I think this option should be included in future version of PHP.

is'nt it something that a lot of PHP programmer would want.

I dont know where to make the request for this feature.

Thanks
bye

cameraman

9:36 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cameraman
Found Something at Zend.

Yikes, I won't be doing that today and tomorrow isn't looking much better. But it is bookmarked and on the list of things to do. Thanks henry0!

eelixduppy

11:11 pm on Mar 30, 2007 (gmt 0)



Extensions are made just for this reason. For those of us who don't want to go crazy, we just use a script with classes or functions that basically does the same job and is much easier to manage and create.

I've been looking at some of the c that revolves around php and its pretty neat stuff. I never had the need to make my own extension, although it must feel like a great achievement. :)

Jaunty Edward

11:14 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



For those of us who don't want to go crazy, we just use a script with classes or functions that basically does the same job and is much easier to manage and create.

do you mean including the class or function file in all sites and all pages? or are you talking about some other easy way?

thanks
bye

eelixduppy

11:17 pm on Mar 30, 2007 (gmt 0)



I just mean including [php.net] the file with the function definitions (or using auto_prepend_file as I said before). It may not be as convenient as having it built into the core of PHP, but isn't that much of a hassle ;)

Jaunty Edward

11:32 pm on Mar 30, 2007 (gmt 0)

10+ Year Member



I agree to eelixduppy that appending the files is a easier way... but I was wondering using that method would mean that every will include all the functions even if we dont use them.

I am wondering how do extension behave? I guess they are only loaded if the functions are called.

Will it make a heavy impact on the performance of website? Considering around 2 Million page views a month across all sites on the server.

thanks
bye

eelixduppy

12:03 am on Mar 31, 2007 (gmt 0)



It would depend on how large the function/class file is.

If you aren't going to be using those functions over and over again, then it would be best to just use include() to get the function definitions before you use them in each script. It will make it more like working with c, where you have to "include" the libraries you want to work with before you can use them.

Jaunty Edward

1:23 am on Mar 31, 2007 (gmt 0)

10+ Year Member



Hi,
I have been trying to use on my local PC with windows and IIS.

Here is what I have done:

created a function and saved the file as myfunc.php I also moved the file to c:/php/includes folder.

edited 2 lines in my php.ini file:

include_path = ".;c:\php\includes"
auto_append_file = myfunc.php

but when I try to use the function, its giving me Fatal error: Call to undefined function: testfunc()

I dont know how to set proper values in php.ini

please help

thanks
bye

[edited by: Jaunty_Edward at 1:24 am (utc) on Mar. 31, 2007]

Jaunty Edward

1:27 am on Mar 31, 2007 (gmt 0)

10+ Year Member



O wow,

its working with

auto_prepend_file = myfunc.inc

thanks anyway.

bye