Jump to content

Where are functions stored?


jeff5656

Recommended Posts

I am about to take the plunge and learn to write some functions to make my coding easier.  However, what I can't seem to find in any of the tutorials is *where* do I store these functions and when do I run them?  DO I have a file called functions.php and then put an include line in every single php page I have?  I that was the case, it would kind of negate the advantgae of having a function - so I think I am not "getting" something.

I figured I would just ask the experts :-)

Link to comment
https://forums.phpfreaks.com/topic/203866-where-are-functions-stored/
Share on other sites

DO I have a file called functions.php and then put an include line in every single php page I have?

 

You can create a file that will act as your "hey, start my application god damit!" (commonly referred to as bootstrap) commonly they use the index.php as it's called by default by your server. This could look like:

 

$page = basename($_GET['page']);

include('library/functions.php');

switch ($page) {
  case 'home':
    include('pages/home.php');
    break;
  
  ..
}

 

I do not advice you would put all your functions in one file though. As your application grows, more functions are added and this can cause an over-head especially for those pages that only use 1 or 2 functions.

 

Over time this will grow to something like:

 

require_once('libraries/somelibrary/frontcontroller.php');
FrontController::start($_REQUEST);

 

And this would load an acronym like MVC where the M toys with a DBAL and they all live in Abstract-ville :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.