Does class function exist

Posted on 2014-08-27
Last Modified: 2014-08-27
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:-

    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
Question by:tonelm54
    LVL 107

    Accepted Solution

    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:

    Author Closing Comment

    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
    LVL 107

    Expert Comment

    by:Ray Paseur
    Yeah, I've had projects like that, too!  Best of luck with it, ~Ray

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now