Function used in 2 files


I have 2 php files and they both use a subset of the same functions. At the moment I am just copying the function into the second file and giving it a slightly different name so I don’t get a conflict. This seems like a waste of effort.

Should I put the common functions in include files, if so how do I pass parameters to the function. If not is there another or better way of doing this.

I know I have been on this site a while but I am self taught and this is the first time I have ever tried to do something like this.


You would create external .php file(s) with your function definition(s) and require it/them when needed. If you use OOP classes, you can set up an autoloader so that definitions are loaded as needed. Note: OOP class(es) are NOT collections of unrelated functions/methods. Don’t simply lump all your functions into one class. The functions/methods in any particular class should be related to the purpose/name of the class they are part of.

The name you give a function should indicate what processing the function does. There would be no need to rename a function unless you change what the function does and if you are regularly editing the code inside a function, it’s a sign that the function is not general purpose and instead of doing something reusable, has the wrong responsibility and probably contains part of your main application code.

Functions should receive all input(s) as call-time parameters and return the result of their processing to the calling code.

If the above doesn’t seem to address what you are doing, you will need to post your function definition(s) and usage.


I guess the larger question currently is, are you doing this procedural or object oriented?


Thanks both for your replies. That makes things clearer.

I have tried just creating include files with just the appropriate function in and it works fine.

I am well into retirement and learning new things is not as easy as it was. I still don’t understand OOP, I need to find some good tutorials / videos about that.

Thanks once again.