Reputation: 177
I'm going to start making an open source forum and I have the following question:
Should I include all the important code in one php page that i require once on each page or having it on each page.
i.e: A code that connects to the MySQL database, another code that secures the input before executing a query.
Upvotes: 1
Views: 334
Reputation: 4446
In fact it doesn't matter, do the way you like. Among the reasons and good examples given in other answers, if you have many files, probably you don't need to load all of them on each request.
Having all in one file assures that on migrate or distribute it you need to move or copy a single file.
If I remember correctly RedBeans is just a single file, while for example Prado has lots of them.
Upvotes: 0
Reputation: 13558
I believe that the nomenclature in programming languages gives you a good starting point for how to structure your code.
function -> does (only) 1 thing, and that good.
class -> collection of functions that belong together
as for the "all in 1 page" part.. you should only have the parser read through code that it will need to accomplish its current task. if a single "page" is supposed to do more than 1 thing, you should only require()
the classes/functions you need when you need them.
Upvotes: 0
Reputation: 681
of course you should put repeating blocks in one file and then include it.
Usually it connect.php
, functions.php
, header.php
, menu.php
and footer.php
Upvotes: 0
Reputation: 76260
In case you are using procedural code you should group functions by their context. For example into: database.php
, input.php
...
In case you are using classes you should have one class in only one file and include it. With classes you can also use the spl_autoload_register
function to autoload classes only when needed.
If you have common code that you know you are always going to need you should create a front controller and include it only there.
Upvotes: 1
Reputation: 4614
You should look into various frameworks that provide tools for implementing better code reuse, bootstrapping, etc. For example, Symfony, Zend Framework, Yii, etc.
Upvotes: 0
Reputation: 634
I personally prefer to do things as modular as possible. That way when you need to change something a few years down the road, you don't have to change it in 100 places.
Upvotes: 2