Kevin Mark
Kevin Mark

Reputation: 1671

Programmatically detecting PEAR in PHP

One of my scripts needs PEAR for some additional functionality. I need a way I can detect if PEAR is installed within PHP itself. Since PEAR.php would be in the include path if properly installed, I suppose I could check for the existence of PEAR.php with file_exists() and then check for the PEAR class inside of it to try and determine if it's actually the file I want. Sounds awfully hackish and unreliable though.

Can anybody suggest a better or improved approach?

Upvotes: 2

Views: 3938

Answers (4)

Björn K
Björn K

Reputation: 126

You could try to use the following setup, rather then using file_exists:

$filePath = stream_resolve_include_path('System.php');
if ($filePath !== false)
{
    require_once('System.php'); // you could use $filePath as well
    echo 'PEAR installed';
}
else
{
    echo 'PEAR not installed';
}

The tricky part is, PEAR will be most likely added to the current include path. Thats the reason why you can use System.php and not /path/to/pear/System.php. This way you can figure out if PEAR is already installed.

Upvotes: 8

ladieu
ladieu

Reputation: 115

<?php 
if(@include_once("System.php"))
{
    echo "Pear is installed";
}
else
{
    echo "Nope";
}
?>

Upvotes: 1

Chris81
Chris81

Reputation: 446

The code above is not correct. You should use it like so:

<?php
include 'System.php';
if(class_exists('System')===true) {
    echo 'PEAR is installed!';
} else {
    echo 'PEAR is not installed :(';
}
?>

Upvotes: 0

Kyle Ross
Kyle Ross

Reputation: 2160

You can check if PEAR is installed by requiring the System.php file to see if the class exists. This method can be done as instructed here: http://pear.php.net/manual/en/installation.checking.php

System.php is shipped with every PEAR installation, so it would be an easy way to detect it.

<?php
require_once 'System.php';
if(class_exists('System')===true) {
   echo 'PEAR is installed!';
} else {
   echo 'PEAR is not installed :(';
}
?>

Hope this helps you!

Upvotes: 4

Related Questions