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
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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.