Are there static functions in PHP?

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

    Are there static functions in PHP?

    I have two files which implement functionality in many of my web pages. Each
    file uses a function named "parseArguments ()" that's critical for each of the
    two files.

    I often include both files into one webpage, which results in a name clash
    for parseArguments( ).

    What I really would like is the concept of static functions from the C
    programming language. A static function is one that has strict file scope:
    no function outisde the containing module sees the function.

    Is there a way to limit the "linkage" of a function to file scope only?

    Thanks!
    Pete
  • IWP506@gmail.com

    #2
    Re: Are there static functions in PHP?

    Do both parseArguments( ) functions do the same thing?

    Comment

    • Jerry Stuckle

      #3
      Re: Are there static functions in PHP?

      Peter Salzman wrote:[color=blue]
      > I have two files which implement functionality in many of my web pages. Each
      > file uses a function named "parseArguments ()" that's critical for each of the
      > two files.
      >
      > I often include both files into one webpage, which results in a name clash
      > for parseArguments( ).
      >
      > What I really would like is the concept of static functions from the C
      > programming language. A static function is one that has strict file scope:
      > no function outisde the containing module sees the function.
      >
      > Is there a way to limit the "linkage" of a function to file scope only?
      >
      > Thanks!
      > Pete[/color]

      Pete,

      No, there aren't any static functions like in C because there isn't the
      concept of separate modules like in C.

      The C equivalent would be:
      #include "myfunc1.c"
      $include "myfunc2.c"

      which would suffer from the same problem. But in C you can compile
      "myfunc1" and "myfunc2" into different modules and have different static
      functions. Unfortunately, that feature isn't available in C++.
      Probably the best you could do to emulate it would be to set up
      "myfunc1.ph p" as an external program and call it - but that's a lot of
      extra effort.

      If all the functions and data in "myfunc1.ph p" are related, you could
      create a class. The same with 'myfunc2.php". Different classes can
      have the same function name. If a class isn't applicable, you'll have
      to change the function names so each is unique.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Mladen Gogala

        #4
        Re: Are there static functions in PHP?

        On Sat, 10 Sep 2005 16:35:05 +0000, Peter Salzman wrote:
        [color=blue]
        >
        > What I really would like is the concept of static functions from the C
        > programming language. A static function is one that has strict file scope:
        > no function outisde the containing module sees the function.[/color]

        What you described is called "private method".

        class A {

        public function now_you_see_me( ) {...}
        private function now_you_dont() {}
        }

        Function "now_you_do nt" will not be visible from any method which is not
        a member function of the class A. That is available as of PHP5.

        --


        Comment

        • Jerry Stuckle

          #5
          Re: Are there static functions in PHP?

          Mladen Gogala wrote:[color=blue]
          > On Sat, 10 Sep 2005 16:35:05 +0000, Peter Salzman wrote:
          >
          >[color=green]
          >>What I really would like is the concept of static functions from the C
          >>programming language. A static function is one that has strict file scope:
          >>no function outisde the containing module sees the function.[/color]
          >
          >
          > What you described is called "private method".
          >
          > class A {
          >
          > public function now_you_see_me( ) {...}
          > private function now_you_dont() {}
          > }
          >
          > Function "now_you_do nt" will not be visible from any method which is not
          > a member function of the class A. That is available as of PHP5.
          >[/color]

          Mladen,

          No, private methods are different. C doesn't have classes, so it can't
          have class members.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Bent Stigsen

            #6
            Re: Are there static functions in PHP?

            Peter Salzman wrote:[color=blue]
            > I have two files which implement functionality in many of my web pages. Each
            > file uses a function named "parseArguments ()" that's critical for each of the
            > two files.
            >
            > I often include both files into one webpage, which results in a name clash
            > for parseArguments( ).
            >
            > What I really would like is the concept of static functions from the C
            > programming language. A static function is one that has strict file scope:
            > no function outisde the containing module sees the function.
            >
            > Is there a way to limit the "linkage" of a function to file scope only?[/color]

            Somewhat. As Jerry mentions, you could define them within a class.
            Sort of a cheap namespace. The "only" nuisance is that you have to
            prefix function-calls with the classname.

            For example:
            [LibA.php]
            <?php
            class LibA {
            function foo() {
            echo __CLASS__, '::', __FUNCTION__, "\n";
            }
            function bar() {
            echo __CLASS__, '::', __FUNCTION__, ' -> ';
            LibA::foo();
            }
            }
            ?>

            [LibB.php]
            <?php
            class LibB {
            function foo() {
            echo __CLASS__, '::', __FUNCTION__, "\n";
            }
            }
            ?>

            [main.php]
            <?php
            include('LibA.p hp');
            include('LibB.p hp');

            LibA::foo();
            LibA::bar();
            LibB::foo();
            ?>

            For "local" variables, you could have an array for each file, or if
            you have PHP5, you can make them static within the classes. But if you
            get a nameclash between class/file-names, then you are back where you
            started.

            To rant a bit and paraphrase a teacher I once had. Use sensible
            naming-conventions for variables, functions, classes, files, etc. and
            enforce it vigorously. Names should never be vague or ambiguous, and
            preferably readable and memorable. (Except when giving examples, I think)

            /Bent

            Comment

            • Mladen Gogala

              #7
              Re: Are there static functions in PHP?

              On Sat, 10 Sep 2005 20:25:55 -0500, Jerry Stuckle wrote:
              [color=blue]
              > Mladen,
              >
              > No, private methods are different. C doesn't have classes, so it can't
              > have class members.[/color]

              Jerry, of course C doesn't have classes. C isn't an OO language while PHP
              is. Also, strictly speaking, there is no linking of PHP scripts, so the
              notion of a symbol local for an object file is not easy to translate to
              PHP. The best thing to do, if he wants to hide functions from the outside
              world, is to use private methods. Yes, that implies classes. PHP and
              C are different to that extent that you can't really compare them feature
              by feature so we have to think outside the box and come up with the best
              functional match. And that is a private method.

              --


              Comment

              • cyberhorse

                #8
                Re: Are there static functions in PHP?

                This is the way I would go about doing this:
                filea.php:
                include_once('p arse.php');
                fileb.php:
                include_once('p arse.php');
                parse.php:
                function parseArguments( ){...}

                php can detect if the file parse.php is already included and not
                include it again, hence not redeclaring the function. Give that a go.

                Comment

                • Obvious

                  #9
                  Re: Are there static functions in PHP?

                  On 11 Sep 2005 01:38:50 -0700, cyberhorse wrote:
                  [color=blue]
                  > This is the way I would go about doing this:[/color]
                  <snip>[color=blue]
                  > include it again, hence not redeclaring the function. Give that a go.[/color]

                  Who are you talking to and what are you talking about? Learn to Quote!

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Are there static functions in PHP?

                    Mladen Gogala wrote:[color=blue]
                    > On Sat, 10 Sep 2005 20:25:55 -0500, Jerry Stuckle wrote:
                    >
                    >[color=green]
                    >>Mladen,
                    >>
                    >>No, private methods are different. C doesn't have classes, so it can't
                    >>have class members.[/color]
                    >
                    >
                    > Jerry, of course C doesn't have classes. C isn't an OO language while PHP
                    > is. Also, strictly speaking, there is no linking of PHP scripts, so the
                    > notion of a symbol local for an object file is not easy to translate to
                    > PHP. The best thing to do, if he wants to hide functions from the outside
                    > world, is to use private methods. Yes, that implies classes. PHP and
                    > C are different to that extent that you can't really compare them feature
                    > by feature so we have to think outside the box and come up with the best
                    > functional match. And that is a private method.
                    >[/color]

                    Mladen,

                    But if you're going to use classes, you don't need to use private methods.

                    The problem he's trying to solve is namespace collision. He has two
                    different functions with the same name and occasionally he needs to
                    include both in a third file. In C this isn't a problem because you can
                    use static functions.

                    In PHP you don't need to use private methods - and may not want to use
                    them, since they can't be called from outside the class. Just having a
                    method as a member of the class is sufficient to resolve the namespace
                    collision. And it can still be called by non-members.

                    Of course, this really isn't different than having different function
                    names in the first place - which may still be the easiest way to go.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: Are there static functions in PHP?

                      cyberhorse wrote:[color=blue]
                      > This is the way I would go about doing this:
                      > filea.php:
                      > include_once('p arse.php');
                      > fileb.php:
                      > include_once('p arse.php');
                      > parse.php:
                      > function parseArguments( ){...}
                      >
                      > php can detect if the file parse.php is already included and not
                      > include it again, hence not redeclaring the function. Give that a go.
                      >[/color]

                      Read again. This won't help as he has "parseArguments " in both
                      filea.php and fileb.php

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • Peter Salzman

                        #12
                        Re: Are there static functions in PHP?

                        Thanks for all the great suggestions!

                        I've re-implemented my PHP "modules" as classes, and it's saved me from
                        the namespace clashes. It's a great solution!

                        I do wish that PHP were a bit more flexible with setting linkage on objects.
                        In general, I love PHP. But there are small things here and there that I
                        really wish were different (I keep wanting to call my arrays @myArray, for
                        example). ;)

                        Pete

                        Comment

                        Working...