Class inheritance

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

    Class inheritance

    Hi,

    Are there any ways to get multiple inheritace in PHP4? For example, I have
    3 parent class, class A, B, and C. I want class X to inherit all those 3
    classes. Consider merging those 3 classes is out of the question, as there
    are class Y which only inherits from class A, etc.

    TIA
  • Dwayne

    #2
    Re: Class inheritance

    Ricky Romaya wrote:[color=blue]
    > Hi,
    >
    > Are there any ways to get multiple inheritace in PHP4? For example, I have
    > 3 parent class, class A, B, and C. I want class X to inherit all those 3
    > classes. Consider merging those 3 classes is out of the question, as there
    > are class Y which only inherits from class A, etc.
    >
    > TIA[/color]

    Yep - check out Object Aggregation (mostly 4.2+)



    - Dwayne

    Comment

    • Chung Leong

      #3
      Re: Class inheritance


      "Ricky Romaya" <something@some where.com> wrote in message
      news:Xns95FA13F 29C723rickyrale xandriacc@66.25 0.146.159...[color=blue]
      > Hi,
      >
      > Are there any ways to get multiple inheritace in PHP4? For example, I have
      > 3 parent class, class A, B, and C. I want class X to inherit all those 3
      > classes. Consider merging those 3 classes is out of the question, as there
      > are class Y which only inherits from class A, etc.
      >
      > TIA[/color]

      Kind of. Example:

      class A {
      var $a;

      function A($var) {
      $this->a = $var;
      }

      function AFunk() {
      echo "AFunk $this->a";
      }
      }

      class B {
      var $b;

      function B($var) {
      $this->b = $var;
      }

      function BFunk() {
      echo "BFunk $this->b";
      }
      }

      class C {

      function C($a, $b) {
      A::A($a);
      B::B($b);
      }

      function AFunk() {
      A::AFunk();
      }

      function BFunk() {
      B::BFunk();
      }
      }

      $obj = new C("Bear", "Tree");
      $obj->AFunk();
      $obj->BFunk();


      Comment

      • Jerry Stuckle

        #4
        Re: Class inheritance

        Dwayne wrote:[color=blue]
        >
        > Ricky Romaya wrote:[color=green]
        > > Hi,
        > >
        > > Are there any ways to get multiple inheritace in PHP4? For example, I have
        > > 3 parent class, class A, B, and C. I want class X to inherit all those 3
        > > classes. Consider merging those 3 classes is out of the question, as there
        > > are class Y which only inherits from class A, etc.
        > >
        > > TIA[/color]
        >
        > Yep - check out Object Aggregation (mostly 4.2+)
        >
        > http://us2.php.net/manual/en/ref.objaggregation.php
        >
        > - Dwayne[/color]

        Aggregation is not inheritance.

        --

        To reply, delete the 'x' from my email
        Jerry Stuckle,
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        Member of Independent Computer Consultants Association - www.icca.org

        Comment

        • Jerry Stuckle

          #5
          Re: Class inheritance

          Chung Leong wrote:[color=blue]
          >
          > "Ricky Romaya" <something@some where.com> wrote in message
          > news:Xns95FA13F 29C723rickyrale xandriacc@66.25 0.146.159...[color=green]
          > > Hi,
          > >
          > > Are there any ways to get multiple inheritace in PHP4? For example, I have
          > > 3 parent class, class A, B, and C. I want class X to inherit all those 3
          > > classes. Consider merging those 3 classes is out of the question, as there
          > > are class Y which only inherits from class A, etc.
          > >
          > > TIA[/color]
          >
          > Kind of. Example:
          >
          > class A {
          > var $a;
          >
          > function A($var) {
          > $this->a = $var;
          > }
          >
          > function AFunk() {
          > echo "AFunk $this->a";
          > }
          > }
          >
          > class B {
          > var $b;
          >
          > function B($var) {
          > $this->b = $var;
          > }
          >
          > function BFunk() {
          > echo "BFunk $this->b";
          > }
          > }
          >
          > class C {
          >
          > function C($a, $b) {
          > A::A($a);
          > B::B($b);
          > }
          >
          > function AFunk() {
          > A::AFunk();
          > }
          >
          > function BFunk() {
          > B::BFunk();
          > }
          > }
          >
          > $obj = new C("Bear", "Tree");
          > $obj->AFunk();
          > $obj->BFunk();[/color]

          It's a way to use aggregation to get around the lack of multiple
          inheritance. The biggest problem is if you change A or B, you also have
          to change C - something that inheritance gets around.

          Additionally, you do not have a "type of" relationship - C is not a
          "type of" either A or B. This isn't as important in PHP, which has very
          little typing. But it can cause problems if you do something like
          serialize() C.

          --

          To reply, delete the 'x' from my email
          Jerry Stuckle,
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          Member of Independent Computer Consultants Association - www.icca.org

          Comment

          • Dwayne

            #6
            Re: Class inheritance

            Jerry Stuckle wrote:[color=blue]
            > Dwayne wrote:
            >[color=green]
            >>Ricky Romaya wrote:
            >>[color=darkred]
            >>>Hi,
            >>>
            >>>Are there any ways to get multiple inheritace in PHP4? For example, I have
            >>>3 parent class, class A, B, and C. I want class X to inherit all those 3
            >>>classes. Consider merging those 3 classes is out of the question, as there
            >>>are class Y which only inherits from class A, etc.
            >>>
            >>>TIA[/color]
            >>
            >>Yep - check out Object Aggregation (mostly 4.2+)
            >>
            >>http://us2.php.net/manual/en/ref.objaggregation.php
            >>
            >>- Dwayne[/color]
            >
            >
            > Aggregation is not inheritance.
            >[/color]

            True, but pragmatically, can you not achieve the same effect as mutliple
            inheritence this way? Or is there something I'm missing?

            Comment

            • chernyshevsky@hotmail.com

              #7
              Re: Class inheritance

              No, that's not aggregation. There are no instances of A or B. $obj is
              treated as though it's either. I would describe it as doing inheritance
              manually. Not something I'd recommend, of course, since it's using an
              undocument trick in the scripting engine.

              Comment

              • Jerry Stuckle

                #8
                Re: Class inheritance

                Dwayne wrote:[color=blue]
                >
                > Jerry Stuckle wrote:[color=green]
                > > Dwayne wrote:
                > >[color=darkred]
                > >>Ricky Romaya wrote:
                > >>
                > >>>Hi,
                > >>>
                > >>>Are there any ways to get multiple inheritace in PHP4? For example, I have
                > >>>3 parent class, class A, B, and C. I want class X to inherit all those 3
                > >>>classes. Consider merging those 3 classes is out of the question, as there
                > >>>are class Y which only inherits from class A, etc.
                > >>>
                > >>>TIA
                > >>
                > >>Yep - check out Object Aggregation (mostly 4.2+)
                > >>
                > >>http://us2.php.net/manual/en/ref.objaggregation.php
                > >>
                > >>- Dwayne[/color]
                > >
                > >
                > > Aggregation is not inheritance.
                > >[/color]
                >
                > True, but pragmatically, can you not achieve the same effect as mutliple
                > inheritence this way? Or is there something I'm missing?[/color]

                Nope. For instance - if you add or delete a method to A or B, you must
                also change C. C is not a "type of" either A or B. This isn't as
                important in PHP as other OO languages, but can cause problems, i.e. if
                you use serialize(). Also, if you need to directly access a variable in
                either A or B (not good programming practice, I admit - use accessor
                methods instead), you need to know the internals of C. In short,
                inheritance is transparent to the user. Aggregation is not.

                Inheritance was created in part to resolve issues like this in
                aggregation. I'm glad to see more OO techniques going into PHP. I
                think it will only get better in this respect.

                --

                To reply, delete the 'x' from my email
                Jerry Stuckle,
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                Member of Independent Computer Consultants Association - www.icca.org

                Comment

                • Dwayne

                  #9
                  Re: Class inheritance

                  Jerry Stuckle wrote:[color=blue][color=green][color=darkred]
                  >>>Aggregatio n is not inheritance.
                  >>>[/color]
                  >>
                  >>True, but pragmatically, can you not achieve the same effect as mutliple
                  >>inheritence this way? Or is there something I'm missing?[/color]
                  >
                  >
                  > Nope. For instance - if you add or delete a method to A or B, you must
                  > also change C. C is not a "type of" either A or B. This isn't as
                  > important in PHP as other OO languages, but can cause problems, i.e. if
                  > you use serialize().[/color]

                  This is true, but you can dynamically bring methods and properties of
                  other classes in, avoiding needing to manually update multiple classes:

                  <?php
                  class Foo
                  {
                  var $foo;

                  function Foo()
                  {
                  $this->initFoo();
                  }

                  function getFoo()
                  {
                  return $this->foo;
                  }

                  function initFoo()
                  {
                  $this->foo = 1;
                  }
                  }

                  class Bar
                  {
                  var $bar;

                  function Bar()
                  {
                  $this->initBar();
                  }

                  function getBar()
                  {
                  return $this->bar;
                  }

                  function initBar()
                  {
                  $this->bar = 2;
                  }
                  }

                  class FooBar
                  {
                  function FooBar()
                  {
                  aggregate_metho ds($this, 'Foo');
                  aggregate_prope rties($this, 'Foo');

                  aggregate_metho ds($this, 'Bar');
                  aggregate_prope rties($this, 'Bar');

                  $this->initFoo();
                  $this->initBar();
                  }
                  }

                  $foobar = new FooBar();
                  print $foobar->getFoo();
                  print $foobar->getBar();
                  ?>

                  Not that it isn't a bit clunky, and with it's own problems (constructors
                  and private methods and properties not being brought in ('private' beind
                  defined as something starting with an underscore)). The whole initFoo()
                  initBar() thing isn't elegant, and strikes me as a potential nuisance
                  down the line, but if you really need to simulate multiple-inheritance,
                  it seems to me that it would work, with a bit of care taken.

                  I have no idea how this would affect serialize(), though.

                  Perhaps I was led astray by this bit in the PHP manual (yes, just
                  pointing my finger and saying "but they did it too!")

                  "In Object Oriented Programming, it is common to see the composition of
                  simple classes (and/or instances) into a more complex one. This is a
                  flexible strategy for building complicated objects and object
                  hierarchies and can function as a dynamic alternative to multiple
                  inheritance"



                  So I should have been more specific about this being aggregation rather
                  than inheritance, but it seems to me the effect is close.
                  [color=blue]
                  > Also, if you need to directly access a variable in
                  > either A or B (not good programming practice, I admit - use accessor
                  > methods instead), you need to know the internals of C. In short,
                  > inheritance is transparent to the user. Aggregation is not.[/color]

                  Agreed on all counts. I generally avoid accessing properties directly,
                  even in child classes, which pretty much takes care of that problem.
                  It's just another maintenance issue I prefer to avoid.
                  [color=blue]
                  > Inheritance was created in part to resolve issues like this in
                  > aggregation. I'm glad to see more OO techniques going into PHP. I
                  > think it will only get better in this respect.[/color]

                  Likewise...I use PHP because it works, but I think I'll actively enjoy PHP5.

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Class inheritance

                    Dwayne wrote:[color=blue]
                    >
                    > Jerry Stuckle wrote:[color=green][color=darkred]
                    > >>>Aggregatio n is not inheritance.
                    > >>>
                    > >>
                    > >>True, but pragmatically, can you not achieve the same effect as mutliple
                    > >>inheritence this way? Or is there something I'm missing?[/color]
                    > >
                    > >
                    > > Nope. For instance - if you add or delete a method to A or B, you must
                    > > also change C. C is not a "type of" either A or B. This isn't as
                    > > important in PHP as other OO languages, but can cause problems, i.e. if
                    > > you use serialize().[/color]
                    >
                    > This is true, but you can dynamically bring methods and properties of
                    > other classes in, avoiding needing to manually update multiple classes:
                    >
                    > <?php
                    > class Foo
                    > {
                    > var $foo;
                    >
                    > function Foo()
                    > {
                    > $this->initFoo();
                    > }
                    >
                    > function getFoo()
                    > {
                    > return $this->foo;
                    > }
                    >
                    > function initFoo()
                    > {
                    > $this->foo = 1;
                    > }
                    > }
                    >
                    > class Bar
                    > {
                    > var $bar;
                    >
                    > function Bar()
                    > {
                    > $this->initBar();
                    > }
                    >
                    > function getBar()
                    > {
                    > return $this->bar;
                    > }
                    >
                    > function initBar()
                    > {
                    > $this->bar = 2;
                    > }
                    > }
                    >
                    > class FooBar
                    > {
                    > function FooBar()
                    > {
                    > aggregate_metho ds($this, 'Foo');
                    > aggregate_prope rties($this, 'Foo');
                    >
                    > aggregate_metho ds($this, 'Bar');
                    > aggregate_prope rties($this, 'Bar');
                    >
                    > $this->initFoo();
                    > $this->initBar();
                    > }
                    > }
                    >
                    > $foobar = new FooBar();
                    > print $foobar->getFoo();
                    > print $foobar->getBar();
                    > ?>
                    >
                    > Not that it isn't a bit clunky, and with it's own problems (constructors
                    > and private methods and properties not being brought in ('private' beind
                    > defined as something starting with an underscore)). The whole initFoo()
                    > initBar() thing isn't elegant, and strikes me as a potential nuisance
                    > down the line, but if you really need to simulate multiple-inheritance,
                    > it seems to me that it would work, with a bit of care taken.
                    >
                    > I have no idea how this would affect serialize(), though.
                    >
                    > Perhaps I was led astray by this bit in the PHP manual (yes, just
                    > pointing my finger and saying "but they did it too!")
                    >
                    > "In Object Oriented Programming, it is common to see the composition of
                    > simple classes (and/or instances) into a more complex one. This is a
                    > flexible strategy for building complicated objects and object
                    > hierarchies and can function as a dynamic alternative to multiple
                    > inheritance"
                    >
                    > http://us2.php.net/manual/en/ref.objaggregation.php
                    >
                    > So I should have been more specific about this being aggregation rather
                    > than inheritance, but it seems to me the effect is close.
                    >[color=green]
                    > > Also, if you need to directly access a variable in
                    > > either A or B (not good programming practice, I admit - use accessor
                    > > methods instead), you need to know the internals of C. In short,
                    > > inheritance is transparent to the user. Aggregation is not.[/color]
                    >
                    > Agreed on all counts. I generally avoid accessing properties directly,
                    > even in child classes, which pretty much takes care of that problem.
                    > It's just another maintenance issue I prefer to avoid.
                    >[color=green]
                    > > Inheritance was created in part to resolve issues like this in
                    > > aggregation. I'm glad to see more OO techniques going into PHP. I
                    > > think it will only get better in this respect.[/color]
                    >
                    > Likewise...I use PHP because it works, but I think I'll actively enjoy PHP5.[/color]


                    This is true - you CAN do it this way. But again, you need to expost
                    the innards of FooBar. With inheritance, you really don't know if
                    FooBar is derived from Foo, Bar, both or neither. It's completely
                    transparent. And, in fact, with inheritance, you could create FooBar as
                    a standalone class - and later extract it into Foo, Bar and derive
                    FooBar from both. The result is 100% transparent to any application.

                    As I said - inheritance is great (if used properly). I've been using it
                    in C++ for around 16 years, and Java for 9 years. And I'm glad to see
                    PHP is implementing more if it, too.
                    --

                    To reply, delete the 'x' from my email
                    Jerry Stuckle,
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    Member of Independent Computer Consultants Association - www.icca.org

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: Class inheritance

                      chernyshevsky@h otmail.com wrote:[color=blue]
                      >
                      > No, that's not aggregation. There are no instances of A or B. $obj is
                      > treated as though it's either. I would describe it as doing inheritance
                      > manually. Not something I'd recommend, of course, since it's using an
                      > undocument trick in the scripting engine.[/color]

                      OK, now that I look more closely, I agree. But I also agree with not
                      recommending it. I hate using "undocument ed feechures". :-)

                      --

                      To reply, delete the 'x' from my email
                      Jerry Stuckle,
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      Member of Independent Computer Consultants Association - www.icca.org

                      Comment

                      • Ricky Romaya

                        #12
                        Re: Class inheritance

                        Hmm, OK guys, I'd hate to reply my own post, but It seemed it's the best
                        way to reply to all your replies.

                        It seemed to me that all your replies hints the use of aggregation and
                        some kind of 'transparent container' technique. I'd have to agree with
                        Jerry about problems of modifying one or more parent class(es), which
                        have to be reflected in the 'inherited' class. I don't realy care about
                        serialize() for the moment, but this is what I'm afraid of. What if the
                        'inherited' class then serves as the parent of another class, or worse,
                        participates on yet another muliple inheritance scheme (i.e. class Z is
                        inherited from Class X, K, L, and class X is inherited from class A, B,
                        C)? (OK, I've heard many says that multiple inheritance are signs of bad
                        design, but what if I really need it)

                        Are there no middle ground here? I've heard that Ruby, a truly OO
                        language, also have single class inheritance restriction (like PHP), but
                        it 'simulates' multiple inheritance with 'mixins'. Anybody know about
                        this and care to elaborate on how to use 'mixins' in PHP4, and also the
                        pros and cons?

                        TIA

                        Comment

                        Working...