3

soo, i am unknowledgeable of ALL best practice.

lets say i call a php file called loader.php with a $_GET['type'] parameter, then after i check if type is actually set i switch the parameter and my logic then does stuff appropriate for $type..

do i create a lot of sub files with the program logic in it or do i just create subfunction (which i have to pass variables if necessary)?

Switch( $_GET['type'] ) { case 'foo': include "logic/foo.php"; break; default: echo "error"; break; }

or is the whole concept totally alien and stupid? i most honestly say that i dont know exactly what i could google to find an answer

Comments
  • 1
    @nregev the funny thing is...2009 when the earth and i were young i "programmed" small browsergame that never saw the light. Now i am doing something else and i thought making it web based solves problems down the line, i already started to write a handler for ajax requests that currently uses two very similar function..40 minutes ago i remembered that i used to include stuff instead of making functions.

    thanks for your input and participating in using devrant as SO surrogate Oo
  • 0
    Separating the different parts into separate files and including them like you did can be fine as long as your application is not really complex. A huge application can be a pain to maintain if you end up with hundreds of files for hundreds of different options.

    Just make sure to not copypaste code in a thousand places and move boilerplate and repeated code into functions as much as you can and you'll surely be fine. Also, you could look into using a templating engine so you don't mishmash PHP code and HTML everywhere and can even also reuse common chunks of HTML if you need to. Good old Smarty can be a good choice, but there are a lot.
  • 1
    @ethernetzero its way worse than you imagine...

    i actually do use smarty (cause i wasnt intelligent enough to bother with other template engines and smarty existed 10 years ago).

    Right now it works like this:

    PHP generates general pages and populates smarty templates according to the language file (which is totally irrelevant for the moment). Then, after a page is loaded as such javascript with the support of jquery loads data tables dynamically from a "loader.php" that just checks if the user is logged in, has appropriate user level and such and then returns a json which in turn then gets used by the jquery/javascript monstrosity to build very simple tables that contain all the data that might get loaded from a database. See other rants of mine on how well i understand javascript :/.

    Thanks for your input
Add Comment