0

So I am running PHP 5.6 on my server and I am trying to check if a function exists, when I do I type this code.

<?php

$functionName = 'SalesWeek';
if (function_exists($functionName)) {
 $functionName();
} else {
 echo "No function exists for ".$functionName."\n";
}

function SalesWeek(){
 echo "Hello!";
}

This fails every single time that I run it. But if I take that exact same code and drop it in something else i.e phptester.net it works just fine. I am using codeigniter so I thought maybe it had to do with that so I tried changing the function to public and private to see if it made a difference. Any ideas?

5
  • 1
    "I tried changing the function to public and private to see if it made a difference" are you sure, you're not talking about a method in a class? functions (outside of a class) have no modifier. Commented Jul 14, 2016 at 19:45
  • It is inside of a class but it is defined exactly as I typed above. I just tried taking it outside of the function then the check works but the call doesn't. I.e, it says function exists but then my call $functionName(); does not work. Commented Jul 14, 2016 at 19:47
  • In case you are trying to check for a method as @tkausl said, check method_exists. Commented Jul 14, 2016 at 19:48
  • method_exists worked once I put in the class name statically. Thanks so much! Commented Jul 14, 2016 at 19:49
  • I not sure why you have codeigniter tag nothing to to with codeigniter there, Commented Jul 14, 2016 at 20:42

3 Answers 3

6

function_exists() only works for top-level functions, not object methods:

<?php

function foo() { echo "foo\n"; }
class bar { function baz() { echo "baz in bar\n"; }}

var_dump(function_exists('foo'));
var_dump(function_exists('baz'));

output:

bool(true)     <--foo
bool(false)    <--baz

Nor will it work for nested functions:

function x() {
   function y() { ... }
}
var_dump(function_exists('y')) -> bool(false)
Sign up to request clarification or add additional context in comments.

Comments

5

If you are using namespaces be sure to include the full qualified name:

function_exists('\myNameSpace\myFunctionName');

Comments

2

Technically functions (in the wild) are not methods (aka "function in a class").

function_exists() does not check for class methods. It checks only for functions in the namespace you are using.

If you want to check for a class' method you need to use method_exists() http://php.net/manual/en/function.method-exists.php

Also there is an order in php is read. And it is top-to-bottom. So in your example above the function you are looking for does not exists before you define it on the last 3 lines of your code.

**BELOW EXAMPLE IS NOT TRUE, SEE THE EDIT **

function_exists('myFunc'); //returns false

function myFunc(){}

function_exists('myFunc'); //returns true

Hope this clears things a bit

EDIT:

I just discovered a very strange behavior (PHP 5.6)

if the function is in the same file:

<?php
function_exists('myFunc'); //returns TRUE

function myFunc(){}

function_exists('myFunc'); //returns TRUE

if it's not in the same file:

<?php
echo function_exists('myFunc') ;//returns FALSE
include 'test2.php';//assume myfunc() is defined in this file
echo function_exists('myFunc');//returns TRUE

SO my first answer above seems to be only partially true. PHP reads your code top to bottom, but it reads whole files. So if you define your function in the same file it will "exist" for php. If it's in another file, that file must first be loaded/included.

1 Comment

About the edit: This is because functions are loaded when a script is loaded. In the first example, the script in the grey block is loaded (so myFunc is loaded), then you call function_exists('myFunc'). In the second example, the script in the grey block is loaded, but test2.php - and by extension myFunc - isn't loaded until you execute the include.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.