Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function_exists returns false every time

Tags:

function

oop

php

I'm trying to check whether a function exists or not but I keep getting false in my if

I try to call the function like this, where $function is the function name:

if (function_exists($this->module->$function))
{
    $this->module->$function($vars);
}
else
{
    echo 'no';
}

The variable module is defined as the class where the function should be called:

$this->module = $module;
$this->module = new $this -> module;

Am I missing something here? Thank you!

like image 987
Fabian Avatar asked Apr 26 '26 10:04

Fabian


2 Answers

Just could figure it out: Using method_exists() solved my problem

method_exists($this->module,$function)

I answered this question on my own for people who may have the same problem!

like image 84
Fabian Avatar answered Apr 29 '26 00:04

Fabian


You need to use method_exists():

if (method_exists($this->module, $function)) {
    // do stuff
}
like image 43
Aron Rotteveel Avatar answered Apr 29 '26 00:04

Aron Rotteveel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!