View Full Version : Creating a PHP Library - Properly linking folders

02-21-2010, 01:28 PM
I've got a project I'm working on which requires some code that is going to end up being very reusable. In order to get the most value out of my code, I want to store it 100% separate from my project.

The library itself is basically two classes + some images. How can I set things up so that in any other project, I can do something like

require '../libs/mylib/mylib.php'

and everything will fall into place? This also includes using those images. Right now, in the library, I simply say "images/image.png". The problem: That path is relative to the file that included the library, not the library. I want to store the images as part of the library.

02-21-2010, 02:13 PM
use require_once(""); or include_once(""); ?

I guess you could use absolute links like "http://example.net/libs/mylib/mylib.php"
But what you've said

require_once ("../libs/mylib/mylib.php")

if I've understood your question, i can't see you having a problem?

02-21-2010, 02:53 PM
That would work if I only wanted to use the library in one place or on one server. I'm having trouble because I want to be able to use this code anywhere on any different server without having to change much more than a configuration path or something.

Note that the include of mylib.php can change. However, mylib.php includes several other files that are part of the library. Because those paths are relative to the file that included mylib.php, it will often be wrong. For example, if I put a demo.php file in the mylib directory and then also try to use the library from a different folder, it won't work in both cases.