PEAR for Beginners
April 10th, 2005
The more I use PEAR the more I want to use PEAR. I think the main reasons that people don’t are (1) they would rather stick to their old guns or (2) they are on a shared host (which is sometimes difficult to get pear up and running). If you want to stick to your old guns fine, but if you are on a shared host and you would like some direction, this is the article for you.
Basically, there are two steps to a standalone (if that is the right term, probably not?) PEAR installation. The first step is too download my little PEAR setup, extract it, and upload the folder named pear to your server (note: I only update the packages when I think of it so this doesn’t have all the newest versions of the packages that I included in it).
The second step is a bit trickier. If PEAR is not installed on your server, you have to modify the include path of each document that uses a PEAR package. This is done by placing the following snippet at the top of every file (everything that I code has a configure file and an application_top file that have settings and required files so I throw it in the top of one of those):
<?php
ini_set("include_path", ini_get("include_path") . PATH_SEPARATOR . /path/to/pear/');
?>
The function ini_set() accepts an ini setting as its first argument and a value as its second. What I am doing in the code piece above is setting the include path to a value of the current path (ini_get() returns the current path) plus the default path separator plus the path to where PEAR is on my server.
That’s it. Place the pear directory in the zip I mentioned above on your server and change the include path on each file that uses a PEAR package. You may be thinking now, “That’s nice, but what can I do with PEAR now that I have it set up?” Well, I have a few upcoming articles on Net_FTP, Benchmark, and DB just to name a few.
If you enjoyed this post, get free updates by email or RSS.
Hi John,
this is (hopefully) what I have been looking for. I have tried working with PEAR, but until now without any succes. I’m on a shared host and although PEAR is installed it’s impossible to include the files!! After a lot of mails with the guys from the host and several unsuccesfull solutions I gave up. Until now, as I’ll try again with your package.
Thanks, Matthijs
Matthijs,
Glad I could help. If you have any problems, let me know. This has worked great for me on several hosts and with several projects. Be sure to update the PEAR classes after you get everything working. The files in the zip above are outdated I am sure. Updating is nothing, just download the new files and upload them over the old files.
Hi John,
As you say at the end of the article: “That’s nice, but what can I do with PEAR now that I have it set up?â€Â, I would like to ask you the following questions.
Could you give a simple example? For example, how would I use the DB class from pear? I tried to find an example on the PEAR site, but that documentation is quite confusing, to say the least ;) And the example given in the php anthology book isn’t very clear either, unforunately.
Maybe you know some good resources to learn more about using pear?
Thanks.
@Matthijs - I have an article on Net_FTP. You can also check out my delicious links tagged with PEAR. These are the resources I use the most. Usually if I can’t figure something out, I just search for PEAR and whatever I want to do on Google. If you look hard enough you can find examples.
I have been using my PHP/MySQL table wrapper of late. I like it better than PEAR, but it is only for MySQL.
John, thanks a lot for the links. You’re obviously totally right that usually some googling will result in some results. But I want to become a programmer and I read somewere that good programmes are lazy ;)
And if you did the hard work already… why not ask?
No, serious, really appreciated. I already see some interesting links on the delicious page.
Could you install packages using this?
This is a pretty old article, but I’m assuming that yes, you could.
You saved me.
Thanks.
Hi John,
Just want to thank you for this. I’ve had trouble getting pear to work on my shared host (they claim it’s installed, but it keeps ‘breaking’ and I’m tired of the back and forth). I put my include path in the .htaccess file and hey presto! One thing: shouldn’t there be a ‘ in front of the /path/to/pear/’ code above?
Thanks again,
Paul
@PaulS - yeah, actually I think I have a missing one at the beginning of the path. That path should be a single quoted string like PATH_SEPARATOR . ‘/path/to/pear/’ I believe.