Oct
06
2008
2

Function Exists

Today i gotten into a problem. I am working on a project that supports multiple modules. And every module has it own functions. But sometimes a function is made double, because both modules need this function.
But you never can be sure both modules are implemented in the website. So, you need to include them twice.

But because PHP gives an error when a function is double redeclared, they have brought up a solution. It’s called function_exists.
Let me show you:

<?php
    if (function_exists('functionA')) {
       echo('It does');
    }
?>

This piece of code checks if functionA exists. But, ofcourse you want to use it so you will never include a double function. So you could use it this way:

<?php
   if (!function_exists('functionA')) {
       function functionA() {
           echo('it does not');
       }
   }
?>

Note the ! before function_exists. This turns an “if” into a “if not”. You can put it in front of every if quote.

Written by Rene Pot in: PHP Problems | Tags: , , , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes