Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Multiple files Vs single file

Tags:

php

I have been playing about with an Ajax/PHP site and I am wondering about the best practices concerning calls to multiple PHP files Vs a single PHP file with many functions in it.

All of these calls are simple database access calls - returning data from a query. It seems a sensible thing to have a single file that opens the database and contains multiple functions, one for each of the calls, however I do not know the best practices in this instance and I am unaware of any security concerns that there may be.

Does anyone know the best practice in this case?

Cheers

BK

like image 252
Bango Skank Avatar asked Oct 31 '25 14:10

Bango Skank


1 Answers

It's a matter of opinion, really. If you haven't got much code, and don't intend to re-use any of it elsewhere, you may as well just have it all in one file. You don't want to fall into the trap of over-complicating what might be a simple setup.

If your code starts to build up and become difficult to follow, you will then be better off splitting it across a number of files based on the sort of task they perform. This is the concept frameworks are based on, where maintaining a more complex application structure is more beneficial to your productivity than fumbling with a monolithic index.php.

And finally: it is vital to take these considerations into account when expecting other people to work with your code.

like image 195
mrlee Avatar answered Nov 02 '25 03:11

mrlee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!