61

How can I check if the function my_function already exists in PHP?

Charles
  • 50,943
  • 13
  • 104
  • 142
bradg
  • 663
  • 1
  • 5
  • 6

6 Answers6

120

Using function_exists:

if(function_exists('my_function')){
    // my_function is defined
}
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
  • 18
    And if you want to check for methods on classes, most people will recommend method_exists but that detects protected/private methods which aren't externally accessible. In that case is_callable is a better option in general. – CaseySoftware Dec 04 '10 at 04:27
  • 3
    @CaseySoftware - For convenience, [here's the doc](http://php.net/manual/en/function.is-callable.php) for ``is_callable``, as this answer was a top result on Google. – Andrew Cheong Sep 28 '12 at 08:37
  • 2
    It's hilarious how semantic and extensive PHP's library is. "how can I tell if a function exists?" "`function_exists`" – Ky - Aug 19 '15 at 17:34
  • how to check this function if exists? `$date = new DateTime(); $date->getTimestamp() ` ? – Disapamok Jun 13 '16 at 06:20
  • if(!function_exists('my_function')) { } – Gene Kelly Jan 02 '17 at 04:57
24

http://php.net/manual/en/function.function-exists.php

<?php
  if (!function_exists('myfunction')) {

     function myfunction()
     {
       //write function statements
     }

}
 ?>
Avinash Saini
  • 1,203
  • 11
  • 10
8
var_dump( get_defined_functions() );

DISPLAYS all existing functions

T.Todua
  • 53,146
  • 19
  • 236
  • 237
3

I want to point out what kitchin has pointed out on php.net:

<?php
// This will print "foo defined"
if (function_exists('foo')) {
  print "foo defined";
} else {
  print "foo not defined";
}
//note even though the function is defined here, it previously was told to have already existed
function foo() {}

If you want to prevent a fatal error and define a function only if it has not been defined, you need to do the following:

<?php
// This will print "defining bar" and will define the function bar
if (function_exists('bar')) {
  print "bar defined";
} else {
  print "defining bar";
  function bar() {}
} 
SeanDowney
  • 17,368
  • 20
  • 81
  • 90
3

And if my_function is in namespace:

namespace MyProject;

function my_function() {
    return 123;
}

You can check if it exists

function_exists( __NAMESPACE__ . '\my_function' );

in the same namespace or

function_exists( '\MyProject\my_function' );

outside of the namespace.

P.S. I know this is a very old question and PHP documentation improved a lot since then, but I believe people still taking a peek here, and this might be helpful.

Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41
1

Checking Multiple function_exists

$arrFun = array('fun1','fun2','fun3');
if(is_array($arrFun)){
  $arrMsg = array();
  foreach ($arrFun as $key => $value) {
    if(!function_exists($value)){
      $arrMsg[] = $value;
    }
  }
  foreach ($arrMsg as $key => $value) {
    echo "{$value} function does not exist <br/>";
  }
}
function fun1(){
}

Output

fun2 function does not exist 
fun3 function does not exist 
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Ram Pukar
  • 1,583
  • 15
  • 17