Subroutines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Little

    Subroutines

    Is this something that has ever been suggested, discussed, debated and/or
    considered? By subroutine I mean a chunk of code that can be called like a
    function but which executes in the global space. Yes, I know an include
    file executes in the global space (if called from there), but when you have
    a number of such chunks of code, it would be a lot cleaner if they could be
    combined in one file.

    --
    Alan Little
    Phorm PHP Form Processor

  • Alvaro G. Vicario

    #2
    Re: Subroutines

    *** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):[color=blue]
    > Is this something that has ever been suggested, discussed, debated and/or
    > considered? By subroutine I mean a chunk of code that can be called like a
    > function but which executes in the global space. Yes, I know an include
    > file executes in the global space (if called from there), but when you have
    > a number of such chunks of code, it would be a lot cleaner if they could be
    > combined in one file.[/color]

    Do you mean functions?

    --
    -+ Álvaro G. Vicario - Burgos, Spain
    +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
    ++ Manda tus dudas al grupo, no a mi buzón
    -+ Send your questions to the group, not to my mailbox
    --

    Comment

    • Justin Koivisto

      #3
      Re: Subroutines

      Alan Little wrote:
      [color=blue]
      > Is this something that has ever been suggested, discussed, debated and/or
      > considered? By subroutine I mean a chunk of code that can be called like a
      > function but which executes in the global space. Yes, I know an include
      > file executes in the global space (if called from there), but when you have
      > a number of such chunks of code, it would be a lot cleaner if they could be
      > combined in one file.
      >[/color]

      Just be creative:

      <?php
      //routines.php

      switch($routine ){
      case 'first':
      // some code

      break;
      case 'second':
      // some different code

      break;
      default:
      // error control code?
      }
      ?>

      <?php
      // other.php
      function myFunc($var1,$v ar2){
      // code

      $routine='secon d';
      include 'routines.php';

      // code
      }

      Now you can do as you want with include a single include file simply by
      setting a flag to indicate which routine to use...

      --
      Justin Koivisto - justin@koivi.co m

      Comment

      • Alan Little

        #4
        Re: Subroutines

        Carved in mystic runes upon the very living rock, the last words of
        Alvaro G. Vicario of comp.lang.php make plain:
        [color=blue]
        > *** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):
        >[color=green]
        >> Is this something that has ever been suggested, discussed, debated
        >> and/or considered? By subroutine I mean a chunk of code that can be
        >> called like a function but which executes in the global space. Yes, I
        >> know an include file executes in the global space (if called from
        >> there), but when you have a number of such chunks of code, it would
        >> be a lot cleaner if they could be combined in one file.[/color]
        >
        > Do you mean functions?[/color]

        By subroutine I mean a chunk of code that can be called like a function
        but which executes in the global space.

        --
        Alan Little
        Phorm PHP Form Processor

        Comment

        • Alan Little

          #5
          Re: Subroutines

          Carved in mystic runes upon the very living rock, the last words of
          Justin Koivisto of comp.lang.php make plain:
          [color=blue]
          > Alan Little wrote:
          >[color=green]
          >> Is this something that has ever been suggested, discussed, debated
          >> and/or considered?[/color]
          >
          > Just be creative:[/color]

          Thank you for your feedback. I have thought of a number of ways of
          approximating the behavior I'm seeking, including what you suggested, but
          nothing is exactly what I want.

          For example, right now I'm working on a template processor which is
          called as an include file. It uses a number of custom tags in the
          template to control its operation. However, the operation of one tag --
          we'll call it Tag A -- needs to be duplicated within the operation of
          another tag -- Tag B. I can either duplicate the Tag A code within the
          Tag B code, or I can have the processor include itself recursively, with
          a flag indicating to process just the specific block of the template,
          rather than the whole thing. Either approach will work, but neither is as
          clean as if I could just put the whole Tag A code into a subroutine and
          call it from Tag B. With the second approach my code has to look
          something like:

          if (!$FlagIsSet) {
          (Tag A) {
          [Tag A code]
          [Set Flag]
          [Recursive include for Tag B functionality]
          }

          (Tag B) {
          [Set Flag]
          [Recursive include for Tag B functionality]
          }

          (Tag C) {
          [Tag C code]
          }
          }

          else {
          [Tag B code]
          }

          It's just not as clean.

          Anyway, I was just wondering if this was something that had been
          considered or discussed in the past. I've run into a need for it a number
          of times.

          --
          Alan Little
          Phorm PHP Form Processor

          Comment

          • Andy Hassall

            #6
            Re: Subroutines

            On Thu, 03 Mar 2005 17:06:10 -0600, Alan Little <alan@n-o-s-p-a-m-phorm.com>
            wrote:
            [color=blue]
            >Carved in mystic runes upon the very living rock, the last words of
            >Alvaro G. Vicario of comp.lang.php make plain:
            >[color=green]
            >> *** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):
            >>[color=darkred]
            >>> Is this something that has ever been suggested, discussed, debated
            >>> and/or considered? By subroutine I mean a chunk of code that can be
            >>> called like a function but which executes in the global space. Yes, I
            >>> know an include file executes in the global space (if called from
            >>> there), but when you have a number of such chunks of code, it would
            >>> be a lot cleaner if they could be combined in one file.[/color]
            >>
            >> Do you mean functions?[/color]
            >
            >By subroutine I mean a chunk of code that can be called like a function
            >but which executes in the global space.[/color]

            I'm having trouble working out what the advantage of this over ordinary
            functions with a "global" statement for the variables it uses would be?

            --
            Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
            <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

            Comment

            • NSpam

              #7
              Re: Subroutines

              Alan Little wrote:[color=blue]
              > Carved in mystic runes upon the very living rock, the last words of
              > Justin Koivisto of comp.lang.php make plain:
              >
              >[color=green]
              >>Alan Little wrote:
              >>
              >>[color=darkred]
              >>>Is this something that has ever been suggested, discussed, debated
              >>>and/or considered?[/color]
              >>
              >>Just be creative:[/color]
              >
              >
              > Thank you for your feedback. I have thought of a number of ways of
              > approximating the behavior I'm seeking, including what you suggested, but
              > nothing is exactly what I want.
              >
              > For example, right now I'm working on a template processor which is
              > called as an include file. It uses a number of custom tags in the
              > template to control its operation. However, the operation of one tag --
              > we'll call it Tag A -- needs to be duplicated within the operation of
              > another tag -- Tag B. I can either duplicate the Tag A code within the
              > Tag B code, or I can have the processor include itself recursively, with
              > a flag indicating to process just the specific block of the template,
              > rather than the whole thing. Either approach will work, but neither is as
              > clean as if I could just put the whole Tag A code into a subroutine and
              > call it from Tag B. With the second approach my code has to look
              > something like:
              >
              > if (!$FlagIsSet) {
              > (Tag A) {
              > [Tag A code]
              > [Set Flag]
              > [Recursive include for Tag B functionality]
              > }
              >
              > (Tag B) {
              > [Set Flag]
              > [Recursive include for Tag B functionality]
              > }
              >
              > (Tag C) {
              > [Tag C code]
              > }
              > }
              >
              > else {
              > [Tag B code]
              > }
              >
              > It's just not as clean.
              >
              > Anyway, I was just wondering if this was something that had been
              > considered or discussed in the past. I've run into a need for it a number
              > of times.
              >[/color]
              require_once() springs to mind <g>

              Comment

              • Alvaro G. Vicario

                #8
                Re: Subroutines

                *** Alan Little escribió/wrote (Thu, 03 Mar 2005 17:06:10 -0600):[color=blue]
                > By subroutine I mean a chunk of code that can be called like a function
                > but which executes in the global space.[/color]

                If you mean a function that can access global variables without previouly
                loading them with the "global" keyword (for whatever the reason), I can
                think of two approaches:

                1) Access variables through the $GLOBALS array.

                2) Create a function that reads the $GLOBALS array and creates local
                variables. Call that function from the other functions that need it.


                --
                -+ Álvaro G. Vicario - Burgos, Spain
                +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
                ++ Manda tus dudas al grupo, no a mi buzón
                -+ Send your questions to the group, not to my mailbox
                --

                Comment

                • Alan Little

                  #9
                  Re: Subroutines

                  Carved in mystic runes upon the very living rock, the last words of Andy
                  Hassall of comp.lang.php make plain:
                  [color=blue]
                  > On Thu, 03 Mar 2005 17:06:10 -0600, Alan Little
                  > <alan@n-o-s-p-a-m-phorm.com> wrote:
                  >[color=green]
                  >>Carved in mystic runes upon the very living rock, the last words of
                  >>Alvaro G. Vicario of comp.lang.php make plain:
                  >>[color=darkred]
                  >>> *** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):
                  >>>
                  >>>> Is this something that has ever been suggested, discussed, debated
                  >>>> and/or considered? By subroutine I mean a chunk of code that can be
                  >>>> called like a function but which executes in the global space. Yes,
                  >>>> I know an include file executes in the global space (if called from
                  >>>> there), but when you have a number of such chunks of code, it would
                  >>>> be a lot cleaner if they could be combined in one file.
                  >>>
                  >>> Do you mean functions?[/color]
                  >>
                  >>By subroutine I mean a chunk of code that can be called like a
                  >>function but which executes in the global space.[/color]
                  >
                  > I'm having trouble working out what the advantage of this over
                  > ordinary functions with a "global" statement for the variables it uses
                  > would be?[/color]

                  When the code doesn't know what variables it will be using.

                  The best example is probably the template processor I'm working on right
                  now. The template contains placeholders for variables; the processor
                  needs to replace them with the appropriate value. It doesn't know until
                  it reads the template what variables it needs. I understand this isn't a
                  difficult problem; as I said, I can think of a number of ways to do it.
                  But they just aren't as clean as code that could simply execute in the
                  space it's called from.

                  The need for this functionality is obviously already recognized, as it's
                  basically what an include file is: centralized code that can be called
                  from multiple locations and executes in the space it's called from. What
                  I'm proposing is to not require a separate file for each such chunk of
                  code.

                  --
                  Alan Little
                  Phorm PHP Form Processor

                  Comment

                  • John

                    #10
                    Re: Subroutines

                    Alan Little wrote:[color=blue]
                    > Is this something that has ever been suggested, discussed, debated and/or
                    > considered? By subroutine I mean a chunk of code that can be called like a
                    > function but which executes in the global space. Yes, I know an include
                    > file executes in the global space (if called from there), but when you have
                    > a number of such chunks of code, it would be a lot cleaner if they could be
                    > combined in one file.[/color]

                    and...
                    [color=blue]
                    > The need for this functionality is obviously already recognized, as it's
                    > basically what an include file is: centralized code that can be called
                    > from multiple locations and executes in the space it's called from. What
                    > I'm proposing is to not require a separate file for each such chunk of
                    > code.[/color]

                    The more you have to include a file to do what you are after, the bigger
                    your total script gets... not what is intended by functions, and not a
                    neat solution, as you say.

                    [color=blue]
                    > The best example is probably the template processor I'm working on right
                    > now. The template contains placeholders for variables; the processor
                    > needs to replace them with the appropriate value. It doesn't know until
                    > it reads the template what variables it needs. I understand this isn't a
                    > difficult problem; as I said, I can think of a number of ways to do it.
                    > But they just aren't as clean as code that could simply execute in the
                    > space it's called from.[/color]


                    Just in case you didn't know this - you can use GLOBAL with a variable:

                    eg:

                    $k = 'a';
                    global ${$k};

                    will give you access to the global 'a'...

                    So you can do what you want.

                    But I agree with your wanting to be able to access the global space
                    more easilly.

                    As much as I don't like globals, PHP is an interpreted script, and under
                    web usage, runs once to complete a single job. So I feel there is an
                    argument in favour of being pratical over writing 'perfect computer
                    science' code.

                    Globals have their place and use :-

                    I want to use constant defines (or thats what they would be in C),
                    rather than using a number throughout my code - something which is
                    considered good pratice.

                    But I don't want to have to 'GLOBAL' 10 constant values in every
                    function that uses them...

                    My suggestion is to add a 'SUPERGLOBAL' operator, that you can use
                    in the global space to add a variable to the superglobal list, eg:

                    SUPERGLOBAL $my_global_a = 1;

                    and then you don't need to do the 'GLOBAL $my_global_a' in every
                    function...

                    John.

                    Comment

                    • Michael Fesser

                      #11
                      Re: Subroutines

                      .oO(John)
                      [color=blue]
                      >Globals have their place and use :-
                      >
                      >I want to use constant defines (or thats what they would be in C),
                      >rather than using a number throughout my code - something which is
                      >considered good pratice.
                      >
                      >But I don't want to have to 'GLOBAL' 10 constant values in every
                      >function that uses them...[/color]

                      Then why don't you use constants?

                      Micha

                      Comment

                      • Andy Hassall

                        #12
                        Re: Subroutines

                        On Fri, 4 Mar 2005 15:02:05 +0100, "Alvaro G. Vicario"
                        <kAlvaroNOSPAMT HANKS@terra.es> wrote:
                        [color=blue]
                        >*** Alan Little escribió/wrote (Thu, 03 Mar 2005 17:06:10 -0600):[color=green]
                        >> By subroutine I mean a chunk of code that can be called like a function
                        >> but which executes in the global space.[/color]
                        >
                        >If you mean a function that can access global variables without previouly
                        >loading them with the "global" keyword (for whatever the reason), I can
                        >think of two approaches:
                        >
                        >1) Access variables through the $GLOBALS array.
                        >
                        >2) Create a function that reads the $GLOBALS array and creates local
                        >variables. Call that function from the other functions that need it.[/color]

                        With the EXTR_REFS option to extract(), can you get very close to
                        automatically creating local variables that are each references to the
                        equivalent global variable by running extract() against the $GLOBALS array? If
                        this works, it ought to result in something nearly indistinguishab le from being
                        in the outer scope?

                        --
                        Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
                        <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

                        Comment

                        • John

                          #13
                          Re: Subroutines

                          Michael Fesser wrote:[color=blue]
                          > .oO(John)
                          >
                          >[color=green]
                          >>Globals have their place and use :-
                          >>
                          >>I want to use constant defines (or thats what they would be in C),
                          >>rather than using a number throughout my code - something which is
                          >>considered good pratice.
                          >>
                          >>But I don't want to have to 'GLOBAL' 10 constant values in every
                          >>function that uses them...[/color]
                          >
                          >
                          > Then why don't you use constants?[/color]

                          Sorry, my mistake - thanks for the tip, will do...

                          But I still like the idea of user defined superglobal's.

                          John.

                          Comment

                          • Alan Little

                            #14
                            Re: Subroutines

                            Carved in mystic runes upon the very living rock, the last words of Andy
                            Hassall of comp.lang.php make plain:
                            [color=blue]
                            > On Fri, 4 Mar 2005 15:02:05 +0100, "Alvaro G. Vicario"
                            > <kAlvaroNOSPAMT HANKS@terra.es> wrote:
                            >[color=green]
                            >>*** Alan Little escribió/wrote (Thu, 03 Mar 2005 17:06:10 -0600):[color=darkred]
                            >>> By subroutine I mean a chunk of code that can be called like a
                            >>> function but which executes in the global space.[/color]
                            >>
                            >>If you mean a function that can access global variables without
                            >>previouly loading them with the "global" keyword (for whatever the
                            >>reason), I can think of two approaches:
                            >>
                            >>1) Access variables through the $GLOBALS array.
                            >>
                            >>2) Create a function that reads the $GLOBALS array and creates local
                            >>variables. Call that function from the other functions that need it.[/color]
                            >
                            > With the EXTR_REFS option to extract(), can you get very close to
                            > automatically creating local variables that are each references to the
                            > equivalent global variable by running extract() against the $GLOBALS
                            > array?[/color]

                            Yes, that works. Thank you. I was concerned it would only change the
                            value in the array itself, but apparently variables in the global space
                            are themselves references to the $GLOBALS array. I don't believe this was
                            always the case; I seem to recall testing this once long ago, and
                            changing $GLOBALS didn't change the variable value.

                            --
                            Alan Little
                            Phorm PHP Form Processor

                            Comment

                            Working...