Tuesday, August 2, 2011

PHP functions for checking the Existance.

Function Existance:

function_exists();


Ex:


if (function_exists('imap_open')) {
    echo 
"IMAP functions are available.\n";
} else {
    echo 
"IMAP functions are not available.\n";
}


Method Existance:

Checks if the class method exists

class ParentClass {

   function
doParent() { }
}

class
ChildClass extends ParentClass { }

$p = new ParentClass();
$c = new ChildClass();

// all return true
var_dump(method_exists($p, 'doParent'));
var_dump(method_exists($c, 'doParent'));


File Existance:


Checks whether a file or directory exists

$filename '/path/to/foo.txt';

if (
file_exists($filename)) {
    echo 
"The file $filename exists";
} else {
    echo 
"The file $filename does not exist";
}


Extension Existance: 


Find out whether an extension is loaded

if (!extension_loaded('gd')) {
    if (!
dl('gd.so')) {
        exit;
    }
}

No comments: