How to call different methods in one PHP file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce W...1

    How to call different methods in one PHP file?

    I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate
    PHP file to support each HTML PHP page. This would be the equivalent of an
    ASP.NET code-behind file but using PHP.

    For one page to do a variety of different things I need to call different
    methods (or functions) in the PHP code file that is dedicated to this purpose.
    How do you do this? How do you call just one method?

    Thanks for your help.
  • Phil Roberts

    #2
    Re: How to call different methods in one PHP file?

    With total disregard for any kind of safety measures "Bruce W...1"
    <bruce@noDirect Email.com> leapt forth and uttered:
    [color=blue]
    > I'm a PHP newbie coming from experience with ASP.NET. I want to
    > have a separate PHP file to support each HTML PHP page. This
    > would be the equivalent of an ASP.NET code-behind file but using
    > PHP.
    >
    > For one page to do a variety of different things I need to call
    > different methods (or functions) in the PHP code file that is
    > dedicated to this purpose. How do you do this? How do you call
    > just one method?
    >
    > Thanks for your help.
    >[/color]

    I'm not entirely sure what you're asking, if you have a class you
    have the choice of either instantiating it as an object and calling
    the method on the object like so:

    $object =& new ClassName($clas s_parameters_if _you_have_any);
    $object->methodName() ;

    Or if the class is composed of static (stand-alone) methods it can be
    called as:

    ClassName::meth odName();

    --
    There is no signature.....

    Comment

    • Matthias Esken

      #3
      Re: How to call different methods in one PHP file?

      "Bruce W...1" <bruce@noDirect Email.com> schrieb:
      [color=blue]
      > For one page to do a variety of different things I need to call different
      > methods (or functions) in the PHP code file that is dedicated to this purpose.
      > How do you do this? How do you call just one method?[/color]

      According to the parameters of the page.

      If I call my PHP page with example.php?tas k=2 the code in example2.php
      will lokk like that:


      $task = (isset($_GET['task']) ? intval($_GET['task']) : 0;
      switch ($task) [
      case 1:
      functionA();
      break;
      case 2:
      functionB();
      break;
      default:
      break;
      }

      Regards,
      Matthias

      Comment

      • Paulus Magnus

        #4
        Re: How to call different methods in one PHP file?


        "Bruce W...1" <bruce@noDirect Email.com> wrote in message
        news:3F824383.3 28B780@noDirect Email.com...[color=blue]
        > I'm a PHP newbie coming from experience with ASP.NET. I want to have a[/color]
        separate[color=blue]
        > PHP file to support each HTML PHP page. This would be the equivalent of[/color]
        an[color=blue]
        > ASP.NET code-behind file but using PHP.
        >
        > For one page to do a variety of different things I need to call different
        > methods (or functions) in the PHP code file that is dedicated to this[/color]
        purpose.[color=blue]
        > How do you do this? How do you call just one method?
        >
        > Thanks for your help.[/color]

        It isn't good programming style to use one PHP file per HTML page. Classes
        should be based on functionality, as should PHP files. If all your pages
        were to do with a membership system then you should have a Member class with
        methods appropriate


        Comment

        • Paulus Magnus

          #5
          Re: How to call different methods in one PHP file?

          "Bruce W...1" <bruce@noDirect Email.com> wrote in message
          news:3F824383.3 28B780@noDirect Email.com...[color=blue]
          > I'm a PHP newbie coming from experience with ASP.NET. I want to have a[/color]
          separate[color=blue]
          > PHP file to support each HTML PHP page. This would be the equivalent of[/color]
          an[color=blue]
          > ASP.NET code-behind file but using PHP.
          >
          > For one page to do a variety of different things I need to call different
          > methods (or functions) in the PHP code file that is dedicated to this[/color]
          purpose.[color=blue]
          > How do you do this? How do you call just one method?
          >
          > Thanks for your help.[/color]

          It isn't good programming style to use one PHP file per HTML page. Classes
          should be based on functionality, as should PHP files. If all your pages
          were to do with a membership system then you should have a Member class with
          methods appropriate to that functionality.

          If you've got a group of functions that perform similar actions you should
          include them in a functions.membe r.inc.php file and include them where you
          need them. You should group functions/methods by functionality rather than
          per page as it will reduce the repetition element, imo.

          Paulus


          Comment

          • Pertti Kosunen

            #6
            Re: How to call different methods in one PHP file?

            "Matthias Esken" <muelleimer2003 nospam@usenetve rwaltung.org> wrote in
            message news:blu856.1gg .1@usenet.esken .de...[color=blue]
            > $task = (isset($_GET['task']) ? intval($_GET['task']) : 0;
            > switch ($task) [[/color]

            This should be:
            switch ($task) {
            [color=blue]
            > case 1:
            > functionA();
            > break;
            > case 2:
            > functionB();
            > break;
            > default:
            > break;
            > }[/color]


            Comment

            • Bruce W...1

              #7
              Re: How to call different methods in one PHP file?

              Paulus Magnus wrote:[color=blue]
              >
              > It isn't good programming style to use one PHP file per HTML page. Classes
              > should be based on functionality, as should PHP files. If all your pages
              > were to do with a membership system then you should have a Member class with
              > methods appropriate to that functionality.
              >
              > If you've got a group of functions that perform similar actions you should
              > include them in a functions.membe r.inc.php file and include them where you
              > need them. You should group functions/methods by functionality rather than
              > per page as it will reduce the repetition element, imo.
              >
              > Paulus[/color]

              =============== =============== =============== =============== ====

              That makes sense I guess, if the functions are used by more than one page.

              That still doesn't answer my original question but thats okay.

              I'm also trying to make sense out of the file names. In a system where every
              file can be named whatever.php there needs to be some organization. So I was
              going to have a codebehind file for each page named whatever.cb.php .

              May I ask what are your file naming conventions? functions.membe r.inc.php is
              one of them.

              Comment

              • Paulus Magnus

                #8
                Re: How to call different methods in one PHP file?

                "Bruce W...1" <bruce@noDirect Email.com> wrote in message
                news:3F832694.C BD99CF@noDirect Email.com...[color=blue]
                > Paulus Magnus wrote:[color=green]
                > >
                > > It isn't good programming style to use one PHP file per HTML page.[/color][/color]
                Classes[color=blue][color=green]
                > > should be based on functionality, as should PHP files. If all your pages
                > > were to do with a membership system then you should have a Member class[/color][/color]
                with[color=blue][color=green]
                > > methods appropriate to that functionality.
                > >
                > > If you've got a group of functions that perform similar actions you[/color][/color]
                should[color=blue][color=green]
                > > include them in a functions.membe r.inc.php file and include them where[/color][/color]
                you[color=blue][color=green]
                > > need them. You should group functions/methods by functionality rather[/color][/color]
                than[color=blue][color=green]
                > > per page as it will reduce the repetition element, imo.
                > >
                > > Paulus[/color]
                >
                > =============== =============== =============== =============== ====
                >
                > That makes sense I guess, if the functions are used by more than one page.
                >
                > That still doesn't answer my original question but thats okay.
                >
                > I'm also trying to make sense out of the file names. In a system where[/color]
                every[color=blue]
                > file can be named whatever.php there needs to be some organization. So I[/color]
                was[color=blue]
                > going to have a codebehind file for each page named whatever.cb.php .
                >
                > May I ask what are your file naming conventions? functions.membe r.inc.php[/color]
                is[color=blue]
                > one of them.[/color]

                I tend to put my classes in a single file called class.name.php and place
                them in a /classes directory on the site. I then usually have two other
                files in the site root, globals.inc.php and common.inc.php. Common.inc.php
                contains all the basic functions I use on multiple pages and also includes
                globals.inc.php . Globals.inc.php is just a list of variables and constants
                for the site that contains MySQL information, site URL and paths, etc. Every
                PHP file on the site then includes common.inc.php, which in turn includes
                globals.inc.php .

                I don't use any external files for functions. If a function is required
                across pages I write a class otherwise it sits in the actual page. I also
                don't have any HTML in my PHP pages, they're pure PHP and all the template
                stuff is in a collection of tpl.name.php files that the pages include as
                necessary. Separating the design from the logic is a big time saver and big
                bug reducer.

                Going back to your original question...

                To call a method from an included file you need to include it, instantiate
                the class and then call the method;

                require ("class.name.ph p");
                $myclass = new FunkyClass;
                $myclass->method_name ();

                If the class was in the same PHP file you wouldn't need the require
                statement.

                If you wanted to call a function from an included file you need to include
                it and call it;

                require ("functions.mem ber.php");
                funky_function ();

                And same again, if the function is in the same PHP file, no require
                statement is needed.

                Paulus


                Comment

                Working...