undefining functions

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

    undefining functions

    I have a master-script that runs for every site I make, and it has a standard
    set of functions, but sometimes I would want to override that function for a
    particular site, but if I redefine it for that site, PHP will complain about
    overriding functions. Since that's a good warning, I wouldn't want to remove
    it, but I would want to "undefine" a function before defining it again.

    like: function_undefi ne("drawbox");

    Or something like that.

    Anyone got any suggestions?

    --
    Sandman[.net]
  • Allan Rydberg

    #2
    Re: undefining functions




    you don't want to define, undefine and redefine functions or
    do you have cpu cycles to waste?

    split your functions.inc into modular entities that you can
    load on request. load the site-specific things at the end.

    request_once('f unc1.inc');
    request_once('f unc2.inc');
    request_once('f unc3.inc');
    request_once('s pec1.inc');
    request_once('s pec2.inc');




    Sandman wrote:
    [color=blue]
    > I have a master-script that runs for every site I make, and it has a standard
    > set of functions, but sometimes I would want to override that function for a
    > particular site, but if I redefine it for that site, PHP will complain about
    > overriding functions. Since that's a good warning, I wouldn't want to remove
    > it, but I would want to "undefine" a function before defining it again.
    >
    > like: function_undefi ne("drawbox");
    >
    > Or something like that.
    >
    > Anyone got any suggestions?
    >[/color]

    Comment

    • Pedro Graca

      #3
      Re: undefining functions

      Sandman wrote:[color=blue]
      > Anyone got any suggestions?[/color]

      <?php
      class Master {
      function func1() { echo 'func1'; return true; }
      function func2() { echo 'func1'; return true; }
      }
      class Site_X {
      function func1() { echo 'X1'; return true; }
      function func2() { echo 'X1'; return true; }
      }

      if (Master::func1( )) Site_X::func2() ;
      ?>
      --
      USENET would be a better place if everybody read: : mail address :
      http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
      http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
      http://www.expita.com/nomime.html : to 10K bytes :

      Comment

      • Mark Henning

        #4
        Re: undefining functions

        "Sandman" <mr@sandman.net > wrote in message
        news:mr-600E6A.13002216 032004@news.fu-berlin.de...[color=blue]
        > I have a master-script that runs for every site I make, and it has a[/color]
        standard[color=blue]
        > set of functions, but sometimes I would want to override that function for[/color]
        a[color=blue]
        > particular site, but if I redefine it for that site, PHP will complain[/color]
        about[color=blue]
        > overriding functions. Since that's a good warning, I wouldn't want to[/color]
        remove[color=blue]
        > it, but I would want to "undefine" a function before defining it again.
        >
        > like: function_undefi ne("drawbox");
        >
        > Or something like that.
        >
        > Anyone got any suggestions?[/color]

        If your _sure_ this is what you want (bearing in mind the overhead involved
        in parsing the
        master-script functions even though they are not used), look into variable
        functions.

        I believe this is possible (correct me if i'm wrong)...

        in master-script:

        function foo_default()
        {
        echo 'blah blah blah';
        }
        $foo = 'foo_default';


        in custom sctipt:

        function foo_redefined()
        {
        echo 'yada yada yada')
        }
        $foo = 'foo_redefined' ; // overwrite variable with new function name.


        to call the function:

        $foo();


        Comment

        • Sandman

          #5
          Re: undefining functions

          In article <c36tj3$91k$1@t aliesin2.netcom .net.uk>,
          "Mark Henning" <mahenning@btop enworld.com> wrote:
          [color=blue]
          > If your _sure_ this is what you want (bearing in mind the overhead involved
          > in parsing the master-script functions even though they are not used), look
          > into variable functions.
          >
          > I believe this is possible (correct me if i'm wrong)...
          >
          > in master-script:
          >
          > function foo_default() {
          > echo 'blah blah blah';
          > }
          > $foo = 'foo_default';
          >
          >
          > in custom sctipt:
          >
          > function foo_redefined() {
          > echo 'yada yada yada')
          > }
          > $foo = 'foo_redefined' ; // overwrite variable with new function name.
          >
          >
          > to call the function:
          >
          > $foo();[/color]

          Aaah, ok - while that might work, that would mean I would have to edit all the
          scripts that use function foo_default(). It's possible, but I'll see if any
          other suggestions come around - thanks!

          --
          Sandman[.net]

          Comment

          • Sandman

            #6
            Re: undefining functions

            In article <c36s86$1ng46h$ 1@ID-203069.news.uni-berlin.de>,
            Pedro Graca <hexkid@hotpop. com> wrote:
            [color=blue]
            > Sandman wrote:[color=green]
            > > Anyone got any suggestions?[/color]
            >
            > <?php
            > class Master {
            > function func1() { echo 'func1'; return true; }
            > function func2() { echo 'func1'; return true; }
            > }
            > class Site_X {
            > function func1() { echo 'X1'; return true; }
            > function func2() { echo 'X1'; return true; }
            > }
            >
            > if (Master::func1( )) Site_X::func2() ;
            > ?>[/color]

            I could use some commentary here.

            After having executed the class Site_X for my site and Master in my initscript
            - would func1() echo "X1"?

            What does the if do?

            --
            Sandman[.net]

            Comment

            • Pedro Graca

              #7
              Re: undefining functions

              Sandman wrote:[color=blue]
              > In article <c36s86$1ng46h$ 1@ID-203069.news.uni-berlin.de>,
              > Pedro Graca <hexkid@hotpop. com> wrote:
              >[color=green]
              >> Sandman wrote:[color=darkred]
              >> > Anyone got any suggestions?[/color]
              >>
              >> <?php
              >> class Master {
              >> function func1() { echo 'func1'; return true; }
              >> function func2() { echo 'func1'; return true; }
              >> }
              >> class Site_X {
              >> function func1() { echo 'X1'; return true; }
              >> function func2() { echo 'X1'; return true; }
              >> }
              >>
              >> if (Master::func1( )) Site_X::func2() ;
              >> ?>[/color]
              >
              > I could use some commentary here.
              >
              > After having executed the class Site_X for my site and Master in my initscript
              > - would func1() echo "X1"?
              >[/color]

              No. With the script above, the function "func1" does not exist.
              You can also define a func1 outside of any class and call it with
              func1().


              You cannot have two functions with the same name. The most similar thing
              available is putting them inside /different/ classes ... of course after
              that their names are "Class::nam e" instead of simply "name".


              /* rewrite of above script */
              <?php
              function func1() { echo 'global func1'; }

              class Site_X {
              function func1() { echo 'Site_X func1'; }
              }

              class Site_Y {
              function func1() { echo 'Site_Y func1 + ', func1(); }
              }

              func1(); // prints "global func1"
              Site_X::func1() ; // prints "Site_X func1"
              Site_Y::func1() ; // prints "Site_Y func1 + global func1"
              ?>

              [color=blue]
              >
              > What does the if do?
              >[/color]

              That was just to show you a way to call the different functions.
              --
              USENET would be a better place if everybody read: : mail address :
              http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
              http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
              http://www.expita.com/nomime.html : to 10K bytes :

              Comment

              • Allan Rydberg

                #8
                Re: undefining functions



                doesn't make sense to me either.
                usually classes are handled in seperate files for flexibility.
                what you do here just pushes the problem one level down the line.

                if you have to check conditions on classe, you can as well
                do conditional includes...




                Pedro Graca wrote:
                [color=blue]
                > Sandman wrote:
                >[color=green]
                >>In article <c36s86$1ng46h$ 1@ID-203069.news.uni-berlin.de>,
                >> Pedro Graca <hexkid@hotpop. com> wrote:
                >>
                >>[color=darkred]
                >>>Sandman wrote:
                >>>
                >>>>Anyone got any suggestions?
                >>>
                >>><?php
                >>>class Master {
                >>> function func1() { echo 'func1'; return true; }
                >>> function func2() { echo 'func1'; return true; }
                >>>}
                >>>class Site_X {
                >>> function func1() { echo 'X1'; return true; }
                >>> function func2() { echo 'X1'; return true; }
                >>>}
                >>>
                >>>if (Master::func1( )) Site_X::func2() ;
                >>>?>[/color]
                >>
                >>I could use some commentary here.
                >>
                >>After having executed the class Site_X for my site and Master in my initscript
                >>- would func1() echo "X1"?
                >>[/color]
                >
                >
                > No. With the script above, the function "func1" does not exist.
                > You can also define a func1 outside of any class and call it with
                > func1().
                >
                >
                > You cannot have two functions with the same name. The most similar thing
                > available is putting them inside /different/ classes ... of course after
                > that their names are "Class::nam e" instead of simply "name".
                >
                >
                > /* rewrite of above script */
                > <?php
                > function func1() { echo 'global func1'; }
                >
                > class Site_X {
                > function func1() { echo 'Site_X func1'; }
                > }
                >
                > class Site_Y {
                > function func1() { echo 'Site_Y func1 + ', func1(); }
                > }
                >
                > func1(); // prints "global func1"
                > Site_X::func1() ; // prints "Site_X func1"
                > Site_Y::func1() ; // prints "Site_Y func1 + global func1"
                > ?>
                >
                >[color=green]
                >>What does the if do?
                >>[/color]
                >
                >
                > That was just to show you a way to call the different functions.[/color]

                Comment

                • Berislav Lopac

                  #9
                  Re: undefining functions

                  Sandman wrote:[color=blue]
                  > I have a master-script that runs for every site I make, and it has a
                  > standard set of functions, but sometimes I would want to override
                  > that function for a particular site, but if I redefine it for that
                  > site, PHP will complain about overriding functions. Since that's a
                  > good warning, I wouldn't want to remove it, but I would want to
                  > "undefine" a function before defining it again.
                  >
                  > like: function_undefi ne("drawbox");
                  >
                  > Or something like that.
                  >
                  > Anyone got any suggestions?[/color]

                  One suggestion is to use objects. Specifically, you could turn your
                  "master-script" into a class, and then instantiate your sites as children of
                  the class, something like:

                  <? /* master-class.php */

                  class BigKahuna {

                  /* here go functions */

                  function OneBigFatMaster Function() {
                  }

                  }

                  ?>

                  <? /* the_site.php */

                  require_once("m aster-class.php");

                  class TheSite extends BigKahuna {

                  function TheSite() {
                  /* this is the constructor function, that gets executed every time
                  you
                  instantiate the class
                  */
                  }

                  function OneBigFatMaster Function() {
                  /* this overrides the function above */
                  }

                  }

                  ?>

                  <? /* index.php for the site */

                  require_once('t he_site.php');

                  $site = new TheSite();

                  ?>

                  That's all, except that instead of calling the functions defined in the two
                  class the usual way, you call them as $this->OneBigFatMaste rFunction().

                  Berislav




                  --
                  If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
                  Groucho, Chico, and Harpo, then Usenet is Zeppo.


                  Comment

                  • Pedro Graca

                    #10
                    Re: undefining functions

                    Allan Rydberg top-posted:[color=blue]
                    > Pedro Graca wrote:[color=green]
                    >> You cannot have two functions with the same name. The most similar thing
                    >> available is putting them inside /different/ classes ... of course after
                    >> that their names are "Class::nam e" instead of simply "name".[/color][/color]

                    Just (mis)using classes as namespaces.
                    [color=blue][color=green]
                    >> /* rewrite of above script */
                    >> <?php
                    >> function func1() { echo 'global func1'; }
                    >>
                    >> class Site_X {
                    >> function func1() { echo 'Site_X func1'; }
                    >> }
                    >>
                    >> class Site_Y {
                    >> function func1() { echo 'Site_Y func1 + ', func1(); }
                    >> }
                    >>
                    >> func1(); // prints "global func1"
                    >> Site_X::func1() ; // prints "Site_X func1"
                    >> Site_Y::func1() ; // prints "Site_Y func1 + global func1"
                    >> ?>[/color][/color]
                    [color=blue]
                    > usually classes are handled in seperate files for flexibility.[/color]

                    OK, doesn't matter if they're in different files.
                    [color=blue]
                    > what you do here just pushes the problem one level down the line.
                    >
                    > if you have to check conditions on classe, you can as well
                    > do conditional includes...[/color]

                    The OP wanted to 'undefine' a function. As that is impossible, I thought
                    maybe creating another of the same name (sort of) would be ok.
                    --
                    USENET would be a better place if everybody read: : mail address :
                    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                    http://www.expita.com/nomime.html : to 10K bytes :

                    Comment

                    • Sandman

                      #11
                      Re: undefining functions

                      In article <c37k1b$ch3$1@l s219.htnet.hr>,
                      "Berislav Lopac" <berislav.lopac @dimedia.hr> wrote:
                      [color=blue]
                      > That's all, except that instead of calling the functions defined in the two
                      > class the usual way, you call them as $this->OneBigFatMaste rFunction().[/color]

                      Ok, I guess this is what the other posted talked about too - I'll try it out -
                      thanks!

                      --
                      Sandman[.net]

                      Comment

                      Working...