I would like to know how i check if a function exists in a class without calling the class.

Say i have a class called Books and it has 2 methods like so:

class Books
{
    public function read()
    {

}

public function find()
{

}
}

How would i find out without loading the class first from another file if the function find() exists or not within that file?

    Method exists seems to require loading the class and as far as i know is unable to just read the file.

      What exactly do you mean by "loading the class" as opposed to "just read the file"?

      EDIT:
      On a hunch, I wonder if you are looking to use [man]reflection[/man]. But then this may be overkill that does not satisfy your requirements.

        My pages are built up using my own mvc so there are controllers and actions. The controller classes are auto loaded on demand.

        Now what i am wanting to do is check if a action(which is a function) exists inside a class and if is does not my router is going to redirect the user to the error 404 page.

        So here is a wee snip of what is going on:

            $thisController = $this->controller."Controller";
            $checkController = new $thisController;
            if (method_exists($checkController, $this->action))
            {
            	echo "true";
            }
            else
            {
            	echo "False";
            }
        

        But because that controller is requred later on by the autoloader i get the following error message:

        Warning: Missing argument 1 for Cabbit\ControllerAction::__construct(), called in /www/totodileparadise.com/html/Libraries/Router.class.php on line 49 and defined in /www/totodileparadise.com/html/Libraries/ControllerAction.class.php on line 10
        
        Notice: Undefined variable: Cabbit in /www/totodileparadise.com/html/Libraries/ControllerAction.class.php on line 12
        true
        Fatal error: Cannot redeclare class albumsController in /www/totodileparadise.com/html/Application/Controllers/albumsController.php on line 61

        So instead of calling the class i am hoping i can just read the contents of the class file for public functions and check for the existence of the function that way.

          Looking at the warning, suppose you change the code to:

              $thisController = $this->controller."Controller";
              if (method_exists($thisController, 'action'))
              {
                  echo "true";
              }
              else
              {
                  echo "False";
              }

          Does that work?

          EDIT:

          Fatal error: Cannot redeclare class albumsController

          Aren't you using say, require_once to avoid redeclaring the class?

            Smaller error but still a error.

            true
            Fatal error: Cannot redeclare class albumsController in /www/totodileparadise.com/html/Application/Controllers/albumsController.php on line 61
            

            So it is detecting the method exists but still the albumsController can not be redeclared.

              Yes the classes are only ever called once like so:

              function __autoload($ClassName)
              {
                  # The directories where we would want to load classes from.
                  # Types:
                  #   Models - One model per database table. Create using script/generate.
                  #   Helpers - Helpful little classes for doing lots of small tasks.
                  #   Libraries - Were Cabbits main libraries are accessed.
              
              $Directories = array(
                  _SitePath."/Application/Helpers/",
                  _SitePath."/Application/Models/",
                  _SitePath."/Libraries/"
               );
              
              # Supported file extentions for Cabbit
              $NameFormats = array(
                  "%s.php",
                  "%s.class.php"
              );
              
              # Removes the namespace part of a class as filenames do not include this
              #   Example
              #   Cabbit/ControllerAction
              #   becomes
              # ControllerAction
              $ClassName = preg_replace("/.*\\\/", "", $ClassName);
              
              # For each class that has been requested by Cabbit or the
              # or the developer such as models we are going to require_once it for use.
              foreach($Directories as $Directory)
              {
                  foreach($NameFormats as $NameFormat)
                  {
              
                      # Set the path to the file were going to load.
                      $Path = $Directory.sprintf($NameFormat, $ClassName);
              
                      # If a file that matches the class name exists were going to load it.
                      if(file_exists($Path))
                      {
                          require_once $Path;
                          return;
                      }
                  }
              }
              }
                jerry_louise wrote:

                So it is detecting the method exists but still the albumsController can not be redeclared.

                Yeah, I realised that the PHP manual does say that "using this function will use any registered autoloaders if the class is not already known." But why is the class being redeclared in the first place?

                  laserlight;10954424 wrote:

                  Yeah, I realised that the PHP manual does say that "using this function will use any registered autoloaders if the class is not already known." But why is the class being redeclared in the first place?

                  If i don't redeclare the class it canny find the class.

                    ChadAyers wrote:

                    Check if a Function exists within a Class WITHOUT calling the function

                    That's what [man]method_exists/man does.

                    The issue isn't about how to do it without calling the function, it's how to do it without creating an instance of the class (at least that's my understanding of the OP's posts).

                      _() i actually worked out that i was already doing something very similar later in the code which i have now noted in the new documentation i've been working on.

                        Write a Reply...