Categories
PHP

PHP: Setting the include_path for quick includes

PHP being the first language I loved and used for a long time, I now use it for all sorts of purposes. I also love and use Python but very often I end up coding in php because I am more habituated to this language than the beautiful python 🙂 Again for some simple tasks, php is loads better than Python. Just imagine you need the md5 hash of a string. Will you import the hashlib first, then go through the md5() and hexdigest() methods ? Nope. I will simply fire off a php interactive shell and use the built in md5() function with an echo.

One of the major problems I was facing that all the external libs I use, I have to copy them into my current working directory or have to use some other techniques to import them into my current script.

To get a permanent work around this problem, I decided to use the include_path directive in php.ini 🙂 I created a directory named “phplib” under my home directory and put all the libs in there.

Next, I opened up /etc/php5/conf.d/ with root access and added a new file named phplib.ini with the following contents:

Now, whenever I need a library loaded, I just type :

That imports the mailer class, even on my interactive php shell 🙂

PS: Well, since the php interpreter always has your home directory as the CWD, you could just use include(“phplib/mailer.php”); in this case. But what about the php module running with apache? The include_path affects that one as well. It helps! But also it has a negative side. Make sure you upload the respective libs to the web server before you deploy your web application on the production box! 😀

One reply on “PHP: Setting the include_path for quick includes”

Comments are closed.