[php5] static and inheritance

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

    [php5] static and inheritance


    How do I use static functions/properties with inheritance? I've found it
    very problematic.

    class Foo
    {
    static function A() {/* ??? */::B(); }
    static function B() {echo 'Foo';}
    };

    class Bar extends Foo
    {
    static function B() {echo 'Bar';}
    }

    I'd like Bar::A() to respect inheritance and output 'Bar', but I can't
    figure out how to make call from base class to static methods of inherited
    class.

    Is there equivalent of $this for static calls? Or something being reverse
    of 'parent'?


    --
    * html {redirect-to: url(http://browsehappy.pl) ;}
  • Janwillem Borleffs

    #2
    Re: [php5] static and inheritance

    porneL wrote:[color=blue]
    > Is there equivalent of $this for static calls? Or something being
    > reverse of 'parent'?[/color]

    Only when you instantiate a class, the parent-child hierarchy fully exists
    and child method have precedence over parent methods.

    If you want a workaround, which is more or less flexible, you could do
    something like the following:

    static function A() {
    foreach (get_declared_c lasses() as $class) {
    if (__CLASS__ == get_parent_clas s($class)) {
    call_user_func( array($class,"B "));
    break;
    }
    }
    }


    JW



    Comment

    • Chung Leong

      #3
      Re: [php5] static and inheritance

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
      news:41c8b562$0 $4453$b83b6cc0@ news.euronet.nl ...[color=blue]
      > porneL wrote:[color=green]
      > > Is there equivalent of $this for static calls? Or something being
      > > reverse of 'parent'?[/color]
      >
      > Only when you instantiate a class, the parent-child hierarchy fully exists
      > and child method have precedence over parent methods.
      >
      > If you want a workaround, which is more or less flexible, you could do
      > something like the following:
      >
      > static function A() {
      > foreach (get_declared_c lasses() as $class) {
      > if (__CLASS__ == get_parent_clas s($class)) {
      > call_user_func( array($class,"B "));
      > break;
      > }
      > }
      > }[/color]

      Errr...your code is exactly equivalent to

      static function A() {
      Bar::B();
      }

      provided that Bar is the first class that inherits Foo.



      Comment

      • Tommy Gildseth

        #4
        Re: [php5] static and inheritance

        Janwillem Borleffs wrote:
        [color=blue]
        > If you want a workaround, which is more or less flexible, you could do
        > something like the following:
        >
        > static function A() {
        > foreach (get_declared_c lasses() as $class) {
        > if (__CLASS__ == get_parent_clas s($class)) {
        > call_user_func( array($class,"B "));
        > break;
        > }
        > }
        > }[/color]

        Wouldn't this just pick the first inherriting class it finds? If there are
        more than one class inherriting Foo, it's difficult to be sure which class
        method would be called.

        --
        Tommy

        Comment

        • Janwillem Borleffs

          #5
          Re: [php5] static and inheritance

          Chung Leong wrote:[color=blue]
          > Errr...your code is exactly equivalent to
          >
          > static function A() {
          > Bar::B();
          > }
          >
          > provided that Bar is the first class that inherits Foo.[/color]

          It's just a suggestion for a method which you can put in any class without
          having to care about its name.


          JW



          Comment

          • Janwillem Borleffs

            #6
            Re: [php5] static and inheritance

            Tommy Gildseth wrote:[color=blue]
            > Wouldn't this just pick the first inherriting class it finds? If
            > there are more than one class inherriting Foo, it's difficult to be
            > sure which class method would be called.[/color]

            Of course it will. I don't say that this method is fullproof, only a more or
            less universal way to implement the method.

            Perhaps a method that verifies whether both class Bar and method Bar::A()
            are defined and Bar extends Foo, is better to handle the dependency, but I
            will leave it up to the OP to select the best solution for his problem.


            JW



            Comment

            • porneL

              #7
              Re: [php5] static and inheritance

              [color=blue][color=green]
              >> static function A() {
              >> foreach (get_declared_c lasses() as $class) {
              >> if (__CLASS__ == get_parent_clas s($class)) {
              >> call_user_func( array($class,"B "));
              >> break;
              >> }
              >> }
              >> }[/color][/color]

              Wrong way. It's emulation of parent::B(). I need "child::B() " or rather
              "current::B ()".


              --
              porneL

              Comment

              • Janwillem Borleffs

                #8
                Re: [php5] static and inheritance

                porneL wrote:[color=blue]
                > Wrong way. It's emulation of parent::B(). I need "child::B() " or
                > rather "current::B ()".[/color]

                When that's the case, why does the following code then print "Bar"?

                <?

                class Foo {
                static function A() {
                foreach (get_declared_c lasses() as $class) {
                if (__CLASS__ == get_parent_clas s($class)) {
                call_user_func( array($class,"B "));
                break;
                }
                }
                }

                static function B() {echo 'Foo';}
                };

                class Bar extends Foo {
                static function B() {echo 'Bar';}
                }

                Bar::A();

                ?>


                JW



                Comment

                • porneL

                  #9
                  Re: [php5] static and inheritance

                  On Wed, 22 Dec 2004 15:23:22 +0100, Janwillem Borleffs <jw@jwscripts.c om>
                  wrote:
                  [color=blue]
                  > porneL wrote:[color=green]
                  >> Wrong way. It's emulation of parent::B(). I need "child::B() " or
                  >> rather "current::B ()".[/color]
                  >
                  > When that's the case, why does the following code then print "Bar"?[/color]

                  Oh, sorry, at first glance I didn't notice the loop.

                  It does, but unfortunetely its pragmatic solution only for example in my
                  post.
                  It doesn't solve general problem of static methods and inheritance.
                  If there are other classes extending Foo, it won't work properly.


                  I haven't found any appropriate language construct, so I assume that is
                  design problem/limitation of PHP.
                  Other OO languages don't require classname for static calls, so base class
                  isn't forced to call itself only.


                  I think that there is a hack to solve this problem - debug_backtrace ()
                  returns classes and call types...

                  --
                  porneL
                  * html {redirect-to: url(http://browsehappy.com );}

                  Comment

                  • Tommy Gildseth

                    #10
                    Re: [php5] static and inheritance

                    porneL wrote:
                    [color=blue]
                    > On Wed, 22 Dec 2004 15:23:22 +0100, Janwillem Borleffs <jw@jwscripts.c om>
                    > wrote:
                    >
                    > I haven't found any appropriate language construct, so I assume that is
                    > design problem/limitation of PHP.
                    > Other OO languages don't require classname for static calls, so base class
                    > isn't forced to call itself only.[/color]

                    Maybe the reason you can't do this, is because the language supposedly
                    doesn't support it. From the PHP manual:

                    "Declaring class members or methods as static, makes them callable from
                    outside the object context. A member or method declared with static can not
                    be accessed with a variable that is an instance of the object and cannot be
                    re-defined in an extending class."


                    --
                    Tommy

                    Comment

                    • agadalam

                      #11
                      Re: [php5] static and inheritance

                      OK, one possible answer to your problem is using debug_bactrace in the
                      following form:

                      class Foo {
                      public static function getStaticCallin gClass(){
                      $debugArray = debug_backtrace ();
                      $className = $debugArray[2]['class'];
                      return $className;
                      }
                      public static function A(){
                      $className = self::getStatic CallingClass();
                      eval($className . "::B();");
                      }

                      public static function B(){
                      echo "Foo";
                      }


                      }

                      class Bar extends Foo {
                      public static function B(){
                      echo "Bar";
                      }
                      }


                      The problem of handling the access of inherited static members and
                      functions is probably a common one in PHP5.

                      It is a matter of philosophy of the language.

                      But I do believe there should be a token which easily addresses this in
                      PHP5.



                      -------------------------------------
                      porneL wrote:


                      [color=blue]
                      > How do I use static functions/properties with inheritance? I've found
                      > it
                      > very problematic.[/color]
                      [color=blue]
                      > class Foo
                      > {
                      > static function A() {/* ??? */::B(); }
                      > static function B() {echo 'Foo';}
                      > };[/color]
                      [color=blue]
                      > class Bar extends Foo
                      > {
                      > static function B() {echo 'Bar';}
                      > }[/color]
                      [color=blue]
                      > I'd like Bar::A() to respect inheritance and output 'Bar', but I
                      > can't
                      > figure out how to make call from base class to static methods of
                      > inherited
                      > class.[/color]
                      [color=blue]
                      > Is there equivalent of $this for static calls? Or something being
                      > reverse
                      > of 'parent'?[/color]





                      ##-----------------------------------------------#
                      Article posted with Web Developer's USENET Archiv

                      no-spam read and post WWW interface to your favorite newsgroup -
                      comp.lang.php - 21435 messages and counting
                      ##-----------------------------------------------##

                      Comment

                      Working...