1
\$\begingroup\$

I have a file called paths.php which is the first file that loads on every page request. I read that I should make a constant of the root folder path but not for every file, but the main point why I did constants for commonly used paths: when the path changes I don't need to go around different files changing require(ROOT . "/path/to/file"); instead all I need is to change the constant value. The path constant list is building up fast and every time I open that file I feel that something is not right and something needs to be changed so I decided to ask my first question here. I heard about autoloading, but by my (small) knowledge it's only for OOP projects (please fix me if I'm wrong)? This project is done without OOP only for practice purposes, I want to make at least one non-oop page and then move on to OOP.

So here is the code, I hope to get criticism, tips, and what should I change or do differently with arguments so I can learn. This is not a full file, just a part of it - modules/templates paths

// 04.
defined("MODULES") or define("MODULES", RESOURCES . "/modules");
defined("FUNCTIONS") or define("FUNCTIONS", MODULES . "/functions.php");
defined("AUTH") or define("AUTH", MODULES . "/authentication");
defined("AUTH_CONTROL") or define("AUTH_CONTROL", AUTH . "/auth-control.php");
defined("SIGN_UP") or define("SIGN_UP", AUTH . "/sign-up.php");
defined("SIGN_IN") or define("SIGN_IN", AUTH . "/sign-in.php");
defined("FORMS") or define("FORMS", MODULES . "/forms");
defined("FORM_CONTROL") or define("FORM_CONTROL", FORMS . "/form-control.php");
defined("FORM_ERRORS") or define("FORM_ERRORS", FORMS . "/form-errors.php");
defined("FORM_VALIDATION_FUNCTIONS") or define("FORM_VALIDATION_FUNCTIONS", FORMS . "/form-validation-functions.php");
defined("UPLOAD_IMG") or define("UPLOAD_IMG", MODULES . "/upload-image.php");

// 05.
// Templates Folder
defined("TEMPLATES") or define("TEMPLATES", RESOURCES . "/templates");
// Main Templates
defined("MAIN_TEMPLATES") or define("MAIN_TEMPLATES", TEMPLATES . "/main-templates");
defined("HEAD") or define("HEAD", MAIN_TEMPLATES . "/head.php");
defined("HEADER") or define("HEADER", MAIN_TEMPLATES . "/header.php");
defined("BANNER") or define("BANNER", MAIN_TEMPLATES . "/banner.php");
defined("FOOTER") or define("FOOTER", MAIN_TEMPLATES . "/footer.php");
defined("JS_FILES") or define("JS_FILES", MAIN_TEMPLATES . "/js-files.php");
// Content Templates
defined("CONTENT_TEMPLATES") or define("CONTENT_TEMPLATES", TEMPLATES . "/content-templates/");
// Form Templates
defined("FORM_TEMPLATES") or define("FORM_TEMPLATES", TEMPLATES . "/form-tmp");
\$\endgroup\$
2
  • \$\begingroup\$ Start using a template engine like Twig \$\endgroup\$ Mar 20, 2021 at 22:08
  • \$\begingroup\$ Yes, I'll but as I wrote in the post I want to try doing it for myself at least one time. \$\endgroup\$
    – Lukas
    Mar 20, 2021 at 22:22

2 Answers 2

3
\$\begingroup\$

If this is the first thing that loads on every page, then the chance of any of these constants being already defined is very small. Why then define them conditionally? These are constants, they should always have the same value. Therefore you can change:

defined("CONSTANT2") or define("CONSTANT2", CONSTANT1 . "/constant2");

to:

define("CONSTANT2", CONSTANT1 . "/constant2");

One of the reasons to define constants conditionally is that you don't want to overwrite constants that are part of PHP. In my opinion there is a better way to prevent this: Choosing longer constant names. So instead of calling a constant HEAD, you could call it TEMPLATE_HEAD. That way the chance of a naming collision is reduced.

Since PHP 5.3 from 2009 is has been possible to define global constants this way:

const CONSTANT2 = CONSTANT1 . "/constant2";

I know it is purely subjective, but I prefer this syntax. Later, when you start using OOP this will be the only syntax for class constants.

Given the above your code could look like this:

// 04.
const FOLDER_MODULES            = FOLDER_RESOURCES . '/modules';
const MODULE_FUNCTIONS          = FOLDER_MODULES . '/functions.php';
const FOLDER_AUTH               = FOLDER_MODULES . '/authentication';
const AUTH_CONTROL              = FOLDER_AUTH . '/auth-control.php';
const AUTH_SIGN_UP              = FOLDER_AUTH . '/sign-up.php';
const AUTH_SIGN_IN              = FOLDER_AUTH . '/sign-in.php';
const FOLDER_FORMS              = FOLDER_MODULES . '/forms';
const FORM_CONTROL              = FOLDER_FORMS . '/form-control.php';
const FORM_ERRORS               = FOLDER_FORMS . '/form-errors.php';
const FORM_VALIDATION_FUNCTIONS = FOLDER_FORMS . '/form-validation-functions.php';
const IMAGE_UPLOAD              = FOLDER_MODULES . '/upload-image.php';

// 05.
// Templates Folder
const FOLDER_TEMPLATES          = FOLDER_RESOURCES . '/templates';
// Main Templates
const FOLDER_MAIN_TEMPLATES     = FOLDER_TEMPLATES . '/main-templates';
const TEMPLATE_HEAD             = FOLDER_MAIN_TEMPLATES . '/head.php';
const TEMPLATE_HEADER           = FOLDER_MAIN_TEMPLATES . '/header.php';
const TEMPLATE_BANNER           = FOLDER_MAIN_TEMPLATES . '/banner.php';
const TEMPLATE_FOOTER           = FOLDER_MAIN_TEMPLATES . '/footer.php';
const TEMPLATE_JAVASCRIPT       = FOLDER_MAIN_TEMPLATES . '/js-files.php';
// Content Templates
const FOLDER_CONTENT_TEMPLATES  = FOLDER_TEMPLATES . '/content-templates/';
// Form Templates
const TEMPLATE_FORM             = FOLDER_TEMPLATES . '/form-tmp';

Somehow I find this much easier on the eyes.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you, i can't upvote because I'm new here, hope someone gonna do that \$\endgroup\$
    – Lukas
    Mar 21, 2021 at 14:15
1
\$\begingroup\$

To avoid any conflicts with existing constants, choosing a longer name like KIKO suggest is sure fine. But we can do even better. Use class constants. Put the class in your own namespace. This not only avoids conflicts, it also allows to leverage the class autoloading mechanism.

class Constants {
  const MODULES = '...';
  ...
}

To give it some structure I would even define multiple classes each containing only a cluster of related constants. And of course name those classes sensibly, not just Constants. That was just to show my point...

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.