[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Does class function exist

I'm trying to write a simple function in a class which will display debug information if the function doesn't exist. However I don't know how to specify $this->myfunction name. What I've been working on is:-

<?php
    class pageHandler {
        function showMain($arrayData) {
            
        }
        
        function callFunction($functionName, $arrayData) {
            if (function_exists($functionName)) {
                call_user_func(array($this, $functionName),$arrayData);
            } else {
                echo "Function $functionName not yet declared<br/>" . print_r($arrayData,true);
            }
        }
    }
    
    $newTest = new pageHandler();
    $newTest->callFunction("showMain",array('para1'=>'val1', 'para2'=>'val2'));
?>

Open in new window


Any ideas how I should be testing function_exists for a class?

Thank you
0
tonelm54
Asked:
tonelm54
  • 2
1 Solution
 
Ray PaseurCommented:
If you're writing the class, you can write the functions.  You do not need to test to see if class methods exist.  All of that "function_exists()" stuff leads to the kind of spaghetti code that made WordPress such a mess for so many years.  We don't do that any more!

If you're still determined to complicate your life, I can hold my nose and point to this:
http://www.php.net/manual/en/function.method-exists.php
0
 
tonelm54Author Commented:
I agree, however the reason I wanted to put the debug information bit in was to display what function Im missing. I need to quickly write a project, and there are a lot of functions I need to write, and as in the past few days its gone from a simple project to quite a massive one I know Im going to miss out functions, and instead of ugly errors I wanted to display a "nicer" message with the information in it.

I know usually I could get the IDE to display the error saying "function not declared", however as it will be multiple programs all feeding in (some I've got no documentation on) I wanted to simply my debugging
0
 
Ray PaseurCommented:
Yeah, I've had projects like that, too!  Best of luck with it, ~Ray
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now