By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,775 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,775 IT Pros & Developers. It's quick & easy.

How to see if a PEAR package exists?

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
..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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.