How to set the path to access files in the procedural system

5

I would like to know how to use path in a procedural system, the correct way to define the paths, and to access the files, and what else is important to know about it. Researching found more examples in OOP, and found nothing here on this subject.

I'm using ../ , and sometimes ../../../../ rsrs so I think it's time to fix it.

I know I can use define , but I was not sure where to include it, and I did not quite understand the operation of DIRECTORY_SEPARATOR , and options. In this question in SO , you have several paths defined, not just the root , and I want to know, using the procedural system, how do I use it. I create a file with all these rules for each directory? How do I access these directories later?

I'll post an example of using path to generate an error log (which was used in this answer ) and if anyone is willing to explain the details, I think it would be interesting to illustrate how it works.

ini_set('error_log', BASE_DIR . DIRECTORY_SEPARATOR . 'logs' . DS . 'php' . DIRECTORY_SEPARATOR . 'PHP_errors-' . date('Ym') . '.log');
    
asked by anonymous 02.10.2015 / 02:31

1 answer

1

Generally I create a define.php file, with the directories, using the previous settings, much like the OS example, always finalizing the definition with a slash.

<?php
define('ROOT_DIR', filter_input(INPUT_SERVER, 'DOCUMENT_ROOT') . '/');
  define('TEMPLATE_DIR', ROOT_DIR . 'class/');
  define('CLASS_DIR', ROOT_DIR . 'class/');
  define('LIB_DIR', ROOT_DIR . 'library/');
  // insira outros diretórios de interesse

In code you can use concatenating without using bar at the beginning.

<?php
include TEMPLATE_DIR . 'template.html';
include LIB_DIR . 'foo/foo.php';

In the sample code, a constant DS is used that is commonly used instead of DIRECTORY_SEPARATOR . Many projects define after checking if it does not exist, because it is a widespread practice, it is necessary to do this verification to avoid errors.

<?php
if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}

However, it is not necessary to use these constants.

  

In Windows both the forward slash (/) and the backslash (\) are used as a path separator character. In other environments, only the forward slash (/).

Source: link

In this way the code could be written.

<?php
ini_set('error_log', BASE_DIR . '/logs/php/PHP_errors-' . date('Ym') . '.log');
    
02.10.2015 / 13:31