How to make these PHP work?

I have the following file structure:

/
+--- inc/
|    +--- lib1.php
+--- inc2/
|    +--- lib2.php
|    +--- view2.php
+--- view1.php

Both view1.php and view2.php files include lib2.php. Lib2.php includes lib1.php. Graphic:

View1.php ---\
             +---> Lib2.php ---> Lib1.php
View2.php ---/

However, I cannot get this to work because the search paths are unstable. If I try to include "../lib1.php", then only view2.php works. If I try to enable "inc / lib1.php", then only view1.php works. In another view, it always complains that the file was not found.

What should be the right way to handle this?

+3
source share
3 answers

If you use php> = 5.3, you can use __DIR__to create a path. If not, use dirname(__FILE__) therefore in lib2.php just put this:

require_once(__DIR__ . '../inc/lib1.php');
+6

, , (, PHP 5.3):

<?php
define('ROOT_INCLUDE_PATH', __DIR__ . '/');

, , , include.

view1.php:

<?php
define('ROOT_INCLUDE_PATH', __DIR__ . '/');
// ...
include ROOT_INCLUDE_PATH . 'inc/lib1.php';
include ROOT_INCLUDE_PATH . 'inc2/lib2.php';

view2.php( , dirname() , ):

<?php
define('ROOT_INCLUDE_PATH', dirname(__DIR__) . '/');
// ...
include ROOT_INCLUDE_PATH . 'inc/lib1.php';
include ROOT_INCLUDE_PATH . 'inc2/lib2.php';

, , - ; , include , , .

+1

, , . $system_path = "/" , .

0

All Articles