Class constructor inheritance

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

    Class constructor inheritance

    I found an odd situation that probably isn't doing what is intended.

    class A {
    function A() {
    // normal constructor for class1
    }

    function B() {
    // a general purpose method
    }
    }

    class B extends A {
    // this class will inherit A::B as a constructor
    }
  • lawrence

    #2
    Re: Class constructor inheritance

    tcadell@savageR esearch.com (Tim Cadell) wrote in message news:<db31a932. 0403251511.5a2b 739b@posting.go ogle.com>...[color=blue]
    > I found an odd situation that probably isn't doing what is intended.
    >
    > class A {
    > function A() {
    > // normal constructor for class1
    > }
    >
    > function B() {
    > // a general purpose method
    > }
    > }
    >
    > class B extends A {
    > // this class will inherit A::B as a constructor
    > }[/color]

    I'm not sure I understand what you are saying. If I understood you,
    you are saying class B picks up class A's constructor. Why would that
    be odd? That is how it works in Java, and, I would guess, most OOP
    languages. The constructors each execute in the order of the class
    hierarchy, starting at the top and working down to the subclass.

    Or did I misunderstand you?

    Comment

    • Karl Heinz Marbaise

      #3
      Re: Class constructor inheritance

      Hi Tim,[color=blue]
      > I found an odd situation that probably isn't doing what is intended.
      >
      > class A {
      > function A() {
      > // normal constructor for class1
      > }
      >
      > function B() {
      > // a general purpose method
      > }
      > }
      >
      > class B extends A {
      > // this class will inherit A::B as a constructor
      > }[/color]
      The inheritance of the construct will result, cause
      your class B has not defined a Constructor.
      If you like to change this you have to override the
      inherited construcotr with your own version....
      That like in C++ ? What is odd on that?

      Kind Regards.
      Karl Heinz
      --
      Dipl.Ing.(FH) Karl Heinz Marbaise | www.marbaise.org
      Jabba Dabba Dooh ;-) | ICQ# 135949029

      Comment

      • lawrence

        #4
        Re: Class constructor inheritance

        Karl Heinz Marbaise <khmarbaise@gmx .de> wrote in message news:<c40q9m$2d 35do$1@ID-68093.news.uni-berlin.de>...[color=blue]
        > Hi Tim,[color=green]
        > > I found an odd situation that probably isn't doing what is intended.
        > >
        > > class A {
        > > function A() {
        > > // normal constructor for class1
        > > }
        > >
        > > function B() {
        > > // a general purpose method
        > > }
        > > }
        > >
        > > class B extends A {
        > > // this class will inherit A::B as a constructor
        > > }[/color]
        > The inheritance of the construct will result, cause
        > your class B has not defined a Constructor.
        > If you like to change this you have to override the
        > inherited construcotr with your own version....
        > That like in C++ ? What is odd on that?[/color]


        But even if he has a constructor in class B, the constructor in class
        A still executes as part of the construction of class B, yes?

        Comment

        • Joshua Beall

          #5
          Re: Class constructor inheritance

          > But even if he has a constructor in class B, the constructor in class[color=blue]
          > A still executes as part of the construction of class B, yes?[/color]

          None of this is what he is observing.

          First of all, the answer to the above question is no. If class B has a
          constructor, and extends class A (which also has a constructor), then class
          A's constructor *will* *not* be called when you create a B object, unless
          you either

          1) There is no constructor for class B
          2) You explicitly call the constructor for A in the constructor for B

          Now, what Tim was saying is this:

          class Shape {
          function Shape () {
          echo "I am a shape!";
          }

          function Square() {
          echo "I have sharp corners!";
          }
          }

          class Square extends Shape{
          // No constructor defined
          }

          Now, the class Square does not have constructor, so it inherits the parent
          constructor, right? Wrong. It has a constructor, Square::Square( ). Doing

          $S = new Square();

          would result in "I have sharp corners!" being output, but not "I am a
          shape!"

          NB: I have not actually tested this, I am just explaining what Tim was
          saying. And it makes sense, based on what I know of PHP internals, that it
          would be this way. In a hard core language, like C++, I doubt it would
          behave like this. That is what he is observing.


          Comment

          • lawrence

            #6
            Re: Class constructor inheritance

            "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message[color=blue]
            > Now, what Tim was saying is this:
            >
            > class Shape {
            > function Shape () {
            > echo "I am a shape!";
            > }
            >
            > function Square() {
            > echo "I have sharp corners!";
            > }
            > }
            >
            > class Square extends Shape{
            > // No constructor defined
            > }
            >
            > Now, the class Square does not have constructor, so it inherits the parent
            > constructor, right? Wrong. It has a constructor, Square::Square( ). Doing
            >
            > $S = new Square();
            >
            > would result in "I have sharp corners!" being output, but not "I am a
            > shape!"[/color]


            That's the kind of bug that would leave me dazed for days. Like, the
            other day I wrote two class methods with the same name, but different
            signatures, and I assumed PHP would tell the difference based on the
            signature. Turns out I was wrong, but it took me awhile to realize
            what the problem was.

            Comment

            • Chung Leong

              #7
              Re: Class constructor inheritance

              "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message
              news:Ut19c.542$ Kp4.42@nwrddc03 .gnilink.net...[color=blue]
              > NB: I have not actually tested this, I am just explaining what Tim was
              > saying. And it makes sense, based on what I know of PHP internals, that[/color]
              it[color=blue]
              > would be this way. In a hard core language, like C++, I doubt it would
              > behave like this. That is what he is observing.[/color]

              I reproduced this behavior on PHP 4.3.4. Pretty weird. I know this was a
              problem in PHP 3 but that was supposed to have been fixed a long time ago.
              According to the manual, "A constructor is a function of the same name as
              the class it is being defined in."



              Comment

              • Joshua Beall

                #8
                Re: Class constructor inheritance

                > That's the kind of bug that would leave me dazed for days. Like, the[color=blue]
                > other day I wrote two class methods with the same name, but different
                > signatures, and I assumed PHP would tell the difference based on the
                > signature. Turns out I was wrong, but it took me awhile to realize
                > what the problem was.[/color]

                I thought PHP would give an error about redefining a function. What does it
                actually do? Just use the first one?


                Comment

                • lawrence

                  #9
                  Re: Class constructor inheritance

                  "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message news:<EAg9c.188 3$Kp4.153@nwrdd c03.gnilink.net >...[color=blue][color=green]
                  > > That's the kind of bug that would leave me dazed for days. Like, the
                  > > other day I wrote two class methods with the same name, but different
                  > > signatures, and I assumed PHP would tell the difference based on the
                  > > signature. Turns out I was wrong, but it took me awhile to realize
                  > > what the problem was.[/color]
                  >
                  > I thought PHP would give an error about redefining a function. What does it
                  > actually do? Just use the first one?[/color]

                  It just used the last one, which wasn't the one I wanted.

                  Comment

                  • Joshua Beall

                    #10
                    Re: Class constructor inheritance

                    > It just used the last one, which wasn't the one I wanted.

                    I think the only way to do this would be to check to see what variables were
                    passed (NB: if they get passed NULL, it will be the same as if they were not
                    passed at all - using isset() won't help :-/ ), then either do and if/else,
                    if/elseif/else, or switch() setup.


                    Comment

                    • lawrence

                      #11
                      Re: Class constructor inheritance

                      "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message news:<Opn9c.416 9$u_2.1796@nwrd dc01.gnilink.ne t>...[color=blue][color=green]
                      > > It just used the last one, which wasn't the one I wanted.[/color]
                      >
                      > I think the only way to do this would be to check to see what variables were
                      > passed (NB: if they get passed NULL, it will be the same as if they were not
                      > passed at all - using isset() won't help :-/ ), then either do and if/else,
                      > if/elseif/else, or switch() setup.[/color]

                      The only way to do what? Do you mean a way to simulate class methods
                      of the same name that act differently depending on their signatures? I
                      suppose that is true, but it is hardly the same. You end up with,
                      complicated methods, with big switch statements, instead of small,
                      elegant class methods that only do one thing. I resorted to if()
                      statements a level up from where you suggest, at the point where I
                      decide which subclass to initialize. The subclasses can all have
                      methods of the same name, which act differently - at least this way
                      the methods stay relatively small, and do only one thing.

                      Comment

                      • Joshua Beall

                        #12
                        Re: Class constructor inheritance

                        "lawrence" <lkrubner@geoci ties.com> wrote in message
                        news:da7e68e8.0 403290800.467c0 c2a@posting.goo gle.com...[color=blue]
                        > I resorted to if()
                        > statements a level up from where you suggest, at the point where I
                        > decide which subclass to initialize. The subclasses can all have
                        > methods of the same name, which act differently - at least this way
                        > the methods stay relatively small, and do only one thing.[/color]

                        Interesting idea. Glad you came up with something that worked. :-)


                        Comment

                        Working...