Salut
Din experienta mea asi face un director unde sunt fisierele cu clasele si alte functii de uz general.
Apoi, un fisier 'common.php' cu instructiuni necesare comune (date conectare, constante, includere clase) pe care il includ in celelalte fisiere php (nu in cele cu clase).
In acest fisier definesc o constanta BASE_ROOT in care e calea absoluta catre directorul Root al site-ului, si o folosesc ca Baza la includere de fisiere pornind de la Root.
Cam asa e "common.php":
Cod: Selectaţi tot
<?php
// start session (if isn't started)
if(!isset($_SESSION)) session_start();
// data to connect to MySQL database (MySQL server, user, password, database name)
$mysql['host'] ='localhost';
$mysql['user'] ='root';
$mysql['pass'] ='';
$mysql['bdname'] ='cms';
//Base Server-Site root path (ex.: /var/www/vhosts/domain.net/public_html)
define('BASE_ROOT', $_SERVER['DOCUMENT_ROOT']);
// other needed constants ..
//get the name of currently executing php file (without extension)
$parent_f = str_replace('.php', '', basename($_SERVER['SCRIPT_FILENAME']));
//include clases according to currently executing php file and create their objects
//files where to use pdo_mysqli class
if(in_array($parent_f, ['file1','other_file','other_php'])){
include BASE_ROOT .'/dir_class/pdo_mysqli.php';
$obsql = new pdo_mysqli($mysql);
}
//files where to use clas_2 class
if(in_array($parent_f, ['file1', 'php_file5'])){
include BASE_ROOT .'/dir_class/clas_2.php';
$ob2 = new clas_2;
}
//files where to use clas_3 class
if(in_array($parent_f, ['other_file','some_other_php'])){
include BASE_ROOT .'/dir_class/clas_3.php';
$ob3 = new clas_3;
}
Iar in fisierele php unde folosesc clase sau include, pun asa (de exemplu in "file1.php"):
Cod: Selectaţi tot
<?php
include_once 'common.php';
//here i can use $obsql and $ob2 objects
//include another php
include BASE_ROOT .'/some_dir/abc.php';
//abc.php can contain other: include(BASE_ROOT .'/some_dir/xyz.php');
}
Pentru includeri in cascada cu fisiere in locatii diferite, pate iti e de folos si fuctia getRootPath() din acest tutorial:
https://coursesweb.net/php-mysql/relativ ... anywhere_t
- Oricum, e bine sa intrebi si pe altii cum procedeaza, pe alte forumuri, mai cu experienta; poate cunosc o tehnica mai buna.