473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

How to see if a PEAR package exists?

I want to know if some particular PEAR package (for example, DB,
XML_RPC) exists. How can I do that in PHP?

Thanks,
Dec 7 '07 #1
6 2298
..oO(AnrDaemon)
>Greetings, Ming.
In reply to Your message dated Friday, December 7, 2007, 22:08:19,
>I want to know if some particular PEAR package (for example, DB,
XML_RPC) exists. How can I do that in PHP?

Try to include them and catch an exception. (As fast and dirty way)
Not that fast, since you also have to write your own error handler to
throw the exception.

Micha
Dec 8 '07 #2
Jerry Stuckle wrote:
including a file which does not exist will not throw an exception.
My general method is:

$old_error_reporting = error_reporting(E_ERROR);
include 'Foo.php';
error_reporting($old_error_reporting);

This will include a file without issuing any warnings if it's missing.

And then you just perform a test to see if something that you know is
defined in Foo.php has successfully been defined. e.g.

echo class_exists('Foo') ? 'Foo.php found' : 'Foo.php not found';

Some will simply recommend checking to see if the file Foo.php exists
before including it, but thanks to the include_paths setting, that's
easier said than done!

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:34.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 8 '07 #3
Toby A Inkster wrote:
Jerry Stuckle wrote:
>including a file which does not exist will not throw an exception.

My general method is:

$old_error_reporting = error_reporting(E_ERROR);
include 'Foo.php';
error_reporting($old_error_reporting);

This will include a file without issuing any warnings if it's missing.

And then you just perform a test to see if something that you know is
defined in Foo.php has successfully been defined. e.g.

echo class_exists('Foo') ? 'Foo.php found' : 'Foo.php not found';

Some will simply recommend checking to see if the file Foo.php exists
before including it, but thanks to the include_paths setting, that's
easier said than done!
Toby,

@include 'Foo.php';

should do the same thing, but I haven't tried it. Rather, I use
require_once on all my files; there's a reason I include them - because
I need them.

But you're right - include paths make things very difficult to determine
if a file exists or not. You could parse the entire include path, but
it's a lot of unnecessary work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 8 '07 #4
On Dec 8, 7:44 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Toby A Inkster wrote:
Jerry Stuckle wrote:
including a file which does not exist will not throw an exception.
My general method is:
$old_error_reporting = error_reporting(E_ERROR);
include 'Foo.php';
error_reporting($old_error_reporting);
This will include a file without issuing any warnings if it's missing.
And then you just perform a test to see if something that you know is
defined in Foo.php has successfully been defined. e.g.
echo class_exists('Foo') ? 'Foo.php found' : 'Foo.php not found';
Some will simply recommend checking to see if the file Foo.php exists
before including it, but thanks to the include_paths setting, that's
easier said than done!

Toby,

@include 'Foo.php';

should do the same thing, but I haven't tried it. Rather, I use
require_once on all my files; there's a reason I include them - because
I need them.

But you're right - include paths make things very difficult to determine
if a file exists or not. You could parse the entire include path, but
it's a lot of unnecessary work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
@include "Foo.php";
if (!class_exists("Foo")) {
// Code to handle missing PEAR file.
}
Dec 9 '07 #5
allain wrote:
On Dec 8, 7:44 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Toby A Inkster wrote:
>>Jerry Stuckle wrote:
including a file which does not exist will not throw an exception.
My general method is:
$old_error_reporting = error_reporting(E_ERROR);
include 'Foo.php';
error_reporting($old_error_reporting);
This will include a file without issuing any warnings if it's missing.
And then you just perform a test to see if something that you know is
defined in Foo.php has successfully been defined. e.g.
echo class_exists('Foo') ? 'Foo.php found' : 'Foo.php not found';
Some will simply recommend checking to see if the file Foo.php exists
before including it, but thanks to the include_paths setting, that's
easier said than done!
Toby,

@include 'Foo.php';

should do the same thing, but I haven't tried it. Rather, I use
require_once on all my files; there's a reason I include them - because
I need them.

But you're right - include paths make things very difficult to determine
if a file exists or not. You could parse the entire include path, but
it's a lot of unnecessary work.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

@include "Foo.php";
if (!class_exists("Foo")) {
// Code to handle missing PEAR file.
}
Which assumes "Foo.php" exists only once in the include path, and
defines the class "Foo".

Also, the '@' in this case hides an important error message; I include
the file because I *need* the file. If it's not there, my code is useless.

Your code is completely unnecessary and causes additional overhead in a
properly designed system.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 10 '07 #6
Jerry Stuckle wrote:
That's where we differ. I don't use optional classes. But that's not
to say there is anything wrong with your way - it's perfectly fine. Just
my personal preference.
Nor do I usually.

But for my Blog/CMS, I like to be able to take advantage of third-party
libraries that might be installed on the server, but without making my
code completely dependent on them. So the code checks to see if they're
available, and if so uses them. If not, then perhaps you miss out on a
cute little feature, but it's no big deal.

Another way to do this is to have some configuration file somewhere with:

$blah_module_is_installed = FALSE;

and later:

if ($blah_module_is_installed)
{
include "blah.php";
blah();
}

but this requires the site administrator to manually enable the blah
extension. Perhaps I have been using Macs too much, but I think that
"detect if cool feature X is available, and if so use it without a fuss"
is a good philosophy.

Of course, there are some PEAR libraries that my CMS absolutely requires,
and in those cases it would of course allow for the error to be raised
when the included file is not found!

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 3 days, 23:42.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 11 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Analysis&Solutions | last post by:
Greetings: Crack open the beer, PEAR DB 1.6.0 is here! (Hey, I'm a bit giddy with excitement that my intense work during the past seven weeks has come to fruition.) For those unfamiliar with...
3
by: junkmail | last post by:
on a win 2k server apparantly it is saying i dont have it. or it cant find it. im using mySQL 4.1 php 4.3.x apache 3.0.53 im new to php and am doing some tutorials, but when i run the sripts...
1
by: DJ Majestik | last post by:
OK, I am new to the whole PEAR/Smarty templating thing. I am trying to setup my directory structure correctly. If someone could weigh in and see if I have this setup "right", I would appreciate it....
2
by: David | last post by:
I have a book which had code that includes the line: require 'DB.php'; I know that this is from the PEAR code modules, but when I downloaded them and unpacked the directory, they "DB.php"...
1
by: CSTechie | last post by:
I've been battling to install PEAR on Windows XP now for too long. I am not sure what I need to do. When I run go-pear.php from the command line, I get the error message as shown at the end. I...
2
by: jhalaseh | last post by:
Hello, Was wondering if anyone could help me out with a bit of wierd prob. I have pear installed in a UNIX box, with PHP 5.1.2. I'm trying to install some packages by running pear install...
1
by: Paul | last post by:
I just installed php 4.4 with apache 2.0 on WIN XP. All is working so far. From: the prompt, I type: go-pear and here's what I get: Loading zlib: ok Using local package: PEAR-stable......ok...
3
by: Anthony Smith | last post by:
I always get this message. No matter what package $ pear install Date PHP Warning: Module 'oci8' already loaded in Unknown on line 0 No releases available for package "pear.php.net/Date"...
0
snowdonkey
by: snowdonkey | last post by:
I'm trying to use the Mail package from PEAR in a script that sends an e-mail over SMTP. The problem is that the Mail class in Mail.php provided by the package isn't found by the script. I'm using:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.