In PHP, How to Check If a Function Exists

In PHP, how do I check if a function exists?

Using function_exists:

if(function_exists('my_function')){
// my_function is defined
}

Is there a way to check if a function exists within a class?

method_exists checks for method of a class for a given object:

Docs Link:
http://www.php.net/method_exists

if(method_exists($SP, $_POST['function'])) {
{
$SP->$_POST['function']();
}
else
{
echo 'function does not exist.';
}

function_exists() and method_exists() are for these checks. First is for regular functions and second for OOP functions.

Check if a function exists in specific file?

There are functions for most things. Check method_exists():

if(method_exists($this->_controller, $this->_url[1])) {
call_user_func_array([$this->_controller, $this->_url[1]]);
}

For other uses see is_callable().

Check if a function exists or not in PHP

Here is a solution. I modified your code so it will work:

<?php
if (isset($_GET['function']) && function_exists($_GET['function'])) {
$func = $_GET['function']; // Set the URL variable to a regular PHP var
$func(); // Do the function
echo "Exists and ran"; // Let the user know the function exists and it was ran
}
else {
echo "Invalid function name called"; // Function does not exists, so output custom error message
}
?>

Check if a function exists in a class before calling call_user_func()

You're looking for method_exists for starters. But what you should check, too is whether or not the method is callable. This is done by the helpfully named is_callable function:

if (method_exists($this->controller, $this->view)
&& is_callable(array($this->controller, $this->view)))
{
call_user_func(
array($this->controller, $this->view)
);
}

But that's just the start of things. Your snippet contains explicit require calls, which suggests you're not using an autoloader.

What's more: all you're doing is check file_exists, not if the class was already loaded. Your code, then, will generate a fatal error if, per chance your snippet gets executed twice with the same values for $this->controller.

Begin fixing this by, in the very least, changing your require to require_once...

PHP: Check for existence and declare function in one line

The only other way I can think of to do this is to have func() declared in an include

function_exists('func') or include('func.php');

Of course, if it's in an include, you can do it much more simply by doing it like this

include_once('func.php');

Checking if function exists

Try it this way around in your constructor of SDBApplication

method_exists($this->Player, 'Play');

How can I check if an object method exists?

You can use the function method_exists to check if the method is existing in a class or not. for example

if(method_exists('CLASS_NAME', 'METHOD_NAME') ) 
echo "it does exist!";
else
echo "nope, it is not there...";

In your code try

if(method_exists($item, 'getProductgroup')){
$productgroupValidation = 0;
if(method_exists($item->getProductgroup(), 'getUuid'))
{
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup)
{
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
}
}


Related Topics



Leave a reply



Submit