How to check the user function existance?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • visweswaran2830
    New Member
    • Nov 2009
    • 92

    How to check the user function existance?

    Hi,
    I created a web-page which contains functions like add(),sub(),div ide().. consider I am passing two value and functions name from first page. Then I received the values from first page and I want to check whether the function is exist or not. If exist means, the resultant value has to produce otherwise it has to display as "Not a valid function"..... How can I achieve this. Is any function to check availability.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    function_exists ()

    if you were to use objects (object methods), you can check against an interface or a class definition.

    Comment

    • visweswaran2830
      New Member
      • Nov 2009
      • 92

      #3
      I cant get clearly. Could you explain with code snippet.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Code:
        if (!function_exists("doSomething"))
        {
            throw new Exception("You can’t do something!");
        }
        Code:
        interface iTest
        {
            public function doSomething();
        }
        
        class Abc
        {
            private $someValue;
        
            public function __construct(iTest obj)
            {
                this->someValue = obj->doSomething();
            }
        }
        
        class Test implements iTest
        {
            public function doSomething()
            {
                return "something";
            }
        }
        
        $y = new Abc(new stdClass); // throws an error
        $y = new Abc(new Test); // doesn’t throw an error

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          For classes you might also use method_exists().

          Comment

          Working...