Proper file references in PHP with includes
Hi,
I'm still not an expert in PHP and PHPStorm. Now I need to know the following: Consider those four local files in the root directory:
./A.php
./B.php
includes/C.php
includes/D.php
Content of A.php:
require_once '/includes/C.php';
Content of B.php:
require '/includes/D.php';
<a href="/B.php">bla</a>
This is proper on my local machine. However, if I deploy them into a folder on the remote host that is not the document root it doesn't work anymore. So how should I deal with it? How should I declare my references so that PHPStorm does not show any warnings?
Is there a best practice how to deal with including and referencing files in a project in general? A practice that will be appreciated by PHPStorm?
Thanks in advance.
Robert
请先登录再写评论。
Hi Robert,
It is not a PhpStorm issue.
That is wrong, especially if you deploy on Linux/MacOS machine.
You provided FULL ABSOLUTE PATH to the C.php (the path starts with /, so PHP interpreter will look for it in the root of the file system). Instead (in your case) you should provide RELATIVE PATH -- in this case PHP interpreter will look for such include file in all available include paths including current path.
http://php.net/manual/en/function.include.php
So .. try this one instead (no leading slash):
or this one (if you want to be more specific)
Thank you.
I read a bit about including but was still confused how to do it best. I know that it has nothing to do with PHPStorm. Though, for development in PHPStorm it is good to know how it can help with all those file references. AFAIK __DIR__ . '/some/file' is not supported. Maybe because I didn't get it or because this so uncommon that PHPStorm did not implement such a complex referencing.
Do you think the way you have provided is the usual, most simple or even best approach? Does one has to distinguish (small/large project, library inclusion, flexiblity requirements)? How far is it supported by PHPStorm? Can I move files and PHPStorm refactores corresponding files?
It is the document root. Or not? E.g. /var/www
include __DIR__ . '/some/file'; works fine in PhpStorm (at least it works for me).
Full absolute path. It starts from file system root. If not sure -- stop breaking your head with theory and do a practical test drive -- create the same file in both "/include/C.php" and "WEBROOT/include/C.php" -- put different text/commands and see which one will be executed.
Everyone chooses their own approach that suits their needs the most. I virtually do no includes at all -- using autoloading for classes. Of course -- there still has to be at least one or two "normal" includes, which is not a problem using relative paths (at the end of the day you should design your app in such way, so when it will be moved on another folder/server it still will be running without changes into include/require statements -- basic portability requirement).
Of course, relative paths have a tiny penalty compared to full absolute path (as PHP interpreter has to search for it in multiple folders), but the difference is tiny if you are not including hunderds of files and your site is not mega-suoer busy when 0.000001 sec makes the difference.
If you are designing your web app using single entry aproach (when all requests are routed via single entry file, e.g. index.php -- like WordPress and many more other frameworks are doing) then you can declare some constant there (e.g. define('ROOT_DIR', __DIR__); ) and use it in all include/require statements (e.g. include ROOT_DIR . '/app/classes/router.php'; )
Andriy, thank you for your thoughts.
I was thinking about reuse of code from project to project since I'm working as service provider creating smaller web applications.
Regards from Austria,
Robert