简体   繁体   中英

File Structure for a PHP Project

My file structure:

/holiday/admin/list.php
/holiday/includes/functions.php   # common functions
/holiday/index.php

# / is the document root
# /holiday/ is a "self-contained" sub-directory
# There are other "self-contained" sub-directories e.g. /promotion/, /international/

In functions.php I have a common function to generate the <head> part of an HTML; also, a function to return an absolute path from the document root. Note my attempt to calculate /holiday/includes/ .

<? function get_path() {
  // Technically, this returns dirname(__FILE__) - $_SERVER['DOCUMENT_ROOT']
  return str_replace($_SERVER['DOCUMENT_ROOT'], "", dirname(__FILE__)); 
} ?>

<? function open_page($head = "", $body_id = "") { ?>
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<? echo get_path() . "/../css/savvyextras.css"; ?>" />
<script type="text/javascript" src="<? echo get_path() . "/../scripts/modernizr.js"; ?>"></script>
...
<? } ?>

functions.php is included this way:

// From list.php
require_once('../includes/functions.php');
open_page(...);

// From index.php
require_once('./includes/functions.php');
open_page(...);

I feel like there must be a more straightforward approach to accomplish the same thing here. Any built-in PHP function for my get_path() ? Maybe I should approach my problem differently?

Note: Some folks suggested using a framework (which is a good thing). But, to help me (and others) understand this whole include-file thing, other non-framework explanations?

Related Discussions :

@Siku-Siku.Com, auto-loading classes with __autoload() won't really help your main problem. Besides, you'll only be able to get the most out of __autoload() if you move to a mainly object-oriented design, which will bring its own challenges.

Currently, the most sensible thing to do would be as @hafichuk suggests. Make one main includes file, say my_funcs.inc.php, and include it at the top of every other page you have. The advantage is that by giving special .inc extensions to your include files, you can distinguish them more easily. Plus, you can use that to block these files in Apache for just an added bit of security.

If I could also mention:
1) I think short tags are risky. They encourage bad coding practices and leave the door wide open for porting nightmares. And they encourage bad coding practices.
2) Since require is a statement, not a function, it should be used like:

require 'my_file.inc.php';

With the type of layout that you currently have, your best bet is to have a single includes.php file which holds all of your require_once calls, then use require_once('../includes.php') (or equivalent location) at the top of each of your entry scripts. It's a pain to setup and maintain, but at least it's all in one place.

If you plan on moving towards using objects instead of functions, then I'd take a look at using __autoload() .

After experimenting more with this and gathering other inputs, I find using constant will do the trick:

# functions.php

define('PREFIX', '/holiday');

<? function open_page($head = "", $body_id = "") { ?>
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<?php echo PREFIX; ?>/css/savvyextras.css"; ?>" />
<script type="text/javascript" src="<?php echo PREFIX; ?>/scripts/modernizr.js"; ?>"></script>
...
<? } ?>

So, if you need to move /holiday/ to a different sub-directory eg /vacation/ , you'd simply need to change one constant ie PREFIX .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM