PHP5 and Classes

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

    PHP5 and Classes

    My OOP knowledge is flaky, at best, so please be patient with me :-)

    I've downloaded the FPDF class from www.fpdf.org as well as some of
    the scripts that are available.

    Each of these scripts use:
    class mine extends FPDF {}
    which is what you'd expect when extending out to a new class.

    What I want/would like to do is to include multiple scripts which
    extend the base class of FPDF. I've managed to daisy-chain these
    classes similar to:
    -------------------------------------------------------------------------
    <?php
    class FPDF {
    function FPDF(){ echo "class FPDF<BR>";}
    function setSize(){ echo "setSize_FPDF() <BR>";}
    }
    class B extends FPDF {
    function B(){ echo "class B<BR>";}
    function setSize(){ echo "setSize_B()<BR >......";parent ::setSize();}
    }
    class C extends B {
    function C(){ echo "class C<BR>";}
    function setLimit(){ echo "setLimit_C()<B R>";}
    }
    class D extends C {
    function D(){ echo "class D<BR>";}
    function PrintLine(){ echo "PrintLine_D()< BR>";}
    }
    class me extends D {
    function me(){ echo "class me<BR>";}
    function setSize()
    {
    echo "setSize_E()<BR >&nbsp;&nbsp;&n bsp;";
    //--- this only gets the class B function
    parent::setSize ();
    }
    }
    //--- test code
    $x = new me();
    $x->FPDF();
    $x->PrintLine();
    $x->setSize();

    /* --- output
    class me
    class FPDF
    PrintLine_D()
    setSize_E()
    setSize_B()
    setSize_FPDF()
    */
    ?>
    -------------------------------------------------------------------------
    Now even *I* know that OOP was never intended to be like this. Is
    there a better/correct way?

    In my code how would I reference the FPDF setSize() method?
    FYI: the setSize is not overriding the base class, it is an internal
    function to that class only.
    ---------------------------------------------------------------
    jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
    ---------------------------------------------------------------
  • Jerry Stuckle

    #2
    Re: PHP5 and Classes

    Jeff North wrote:
    My OOP knowledge is flaky, at best, so please be patient with me :-)
    >
    I've downloaded the FPDF class from www.fpdf.org as well as some of
    the scripts that are available.
    >
    Each of these scripts use:
    class mine extends FPDF {}
    which is what you'd expect when extending out to a new class.
    >
    What I want/would like to do is to include multiple scripts which
    extend the base class of FPDF. I've managed to daisy-chain these
    classes similar to:
    -------------------------------------------------------------------------
    <?php
    class FPDF {
    function FPDF(){ echo "class FPDF<BR>";}
    function setSize(){ echo "setSize_FPDF() <BR>";}
    }
    class B extends FPDF {
    function B(){ echo "class B<BR>";}
    function setSize(){ echo "setSize_B()<BR >......";parent ::setSize();}
    }
    class C extends B {
    function C(){ echo "class C<BR>";}
    function setLimit(){ echo "setLimit_C()<B R>";}
    }
    class D extends C {
    function D(){ echo "class D<BR>";}
    function PrintLine(){ echo "PrintLine_D()< BR>";}
    }
    class me extends D {
    function me(){ echo "class me<BR>";}
    function setSize()
    {
    echo "setSize_E()<BR >&nbsp;&nbsp;&n bsp;";
    //--- this only gets the class B function
    parent::setSize ();
    }
    }
    //--- test code
    $x = new me();
    $x->FPDF();
    $x->PrintLine();
    $x->setSize();
    >
    /* --- output
    class me
    class FPDF
    PrintLine_D()
    setSize_E()
    setSize_B()
    setSize_FPDF()
    */
    ?>
    -------------------------------------------------------------------------
    Now even *I* know that OOP was never intended to be like this. Is
    there a better/correct way?
    >
    In my code how would I reference the FPDF setSize() method?
    FYI: the setSize is not overriding the base class, it is an internal
    function to that class only.
    ---------------------------------------------------------------
    jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
    ---------------------------------------------------------------
    Jeff,

    The quick answer is - you don't, at least directly.

    In OO, inheritance is "hidden" - the fact you are using it is not known
    to the code accessing the class, nor should it care. The interface to
    your "me" class is the public functions in "me", plus all the public
    functions in it's base class "D" (which, in turn, has an interface which
    includes the public functions in "C", etc.). But your code really
    doesn't know about "D", "C", etc. - OO has abstracted that for you.

    You're overriding of setSize is correct. "me" should do its work, then
    call its base class's setSize (or call then do its work), etc. That way
    each class operates on its own data, and calls its base class to work on
    further data. This allows you to change your class hierarchy with
    (almost) complete freedom without having to change the program. For
    instance, you could compress everything into a single class "me" - and
    your program would still work.

    Now - you want to just call setSize in class FPDF. OO doesn't allow
    this because abstraction says class "FPDF" doesn't actually exist (the
    *function* FPDF may just be a function in class "me" for all it cares).

    So you can't do it directly. However, you *could* create a function in
    FPDF with another name (i.e. setSizeFPDF) to do the same work. This
    will add the new function to your public interface and you can call it
    by itself.

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

    Comment

    • Jeff North

      #3
      Re: PHP5 and Classes

      On Wed, 12 Jul 2006 10:30:05 -0400, in comp.lang.php Jerry Stuckle
      <jstucklex@attg lobal.net>
      <M6qdnd-IMIXBmijZnZ2dnU VZ_vadnZ2d@comc ast.comwrote:
      >| Jeff North wrote:
      >| My OOP knowledge is flaky, at best, so please be patient with me :-)
      >| >
      >| I've downloaded the FPDF class from www.fpdf.org as well as some of
      >| the scripts that are available.
      >| >
      >| Each of these scripts use:
      >| class mine extends FPDF {}
      >| which is what you'd expect when extending out to a new class.
      >| >
      >| What I want/would like to do is to include multiple scripts which
      >| extend the base class of FPDF. I've managed to daisy-chain these
      >| classes similar to:
      >| -------------------------------------------------------------------------
      [snip example code]
      >| -------------------------------------------------------------------------
      >| Now even *I* know that OOP was never intended to be like this. Is
      >| there a better/correct way?
      >| >
      >| In my code how would I reference the FPDF setSize() method?
      >| FYI: the setSize is not overriding the base class, it is an internal
      >| function to that class only.
      >|
      >| Jeff,
      >|
      >| The quick answer is
      It was quick - I only posted this about 20min ago :-)
      >| - you don't, at least directly.
      >|
      >| In OO, inheritance is "hidden" - the fact you are using it is not known
      >| to the code accessing the class, nor should it care. The interface to
      >| your "me" class is the public functions in "me", plus all the public
      >| functions in it's base class "D" (which, in turn, has an interface which
      >| includes the public functions in "C", etc.). But your code really
      >| doesn't know about "D", "C", etc. - OO has abstracted that for you.
      That makes sense :-)
      I've never seen any example daisy-chain classes before.
      >| You're overriding of setSize is correct. "me" should do its work, then
      >| call its base class's setSize (or call then do its work), etc. That way
      >| each class operates on its own data, and calls its base class to work on
      >| further data. This allows you to change your class hierarchy with
      >| (almost) complete freedom without having to change the program. For
      >| instance, you could compress everything into a single class "me" - and
      >| your program would still work.
      Just as I thought. I need to go into each of these script files and
      alter the function names so that they are not overriding each other.
      >| Now - you want to just call setSize in class FPDF. OO doesn't allow
      >| this because abstraction says class "FPDF" doesn't actually exist (the
      >| *function* FPDF may just be a function in class "me" for all it cares).
      >|
      >| So you can't do it directly. However, you *could* create a function in
      >| FPDF with another name (i.e. setSizeFPDF) to do the same work. This
      >| will add the new function to your public interface and you can call it
      >| by itself.
      Many thanks for your help.
      ---------------------------------------------------------------
      jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
      ---------------------------------------------------------------

      Comment

      • ybakos

        #4
        Re: PHP5 and Classes


        I don't quite get what you're trying to accomplish (but I'm not that
        bright).

        You should avoid deep inheritance heirarchies.

        Or perhaps you shouldn't override the methods in this manner, since you
        are trying to achieve the ability to 'reach back' up the inheritance
        chain to call the 'Grandparent' method.

        Not sure if this helps.

        Cheers,
        Yong



        Jeff North wrote:
        My OOP knowledge is flaky, at best, so please be patient with me :-)
        >
        I've downloaded the FPDF class from www.fpdf.org as well as some of
        the scripts that are available.
        >
        Each of these scripts use:
        class mine extends FPDF {}
        which is what you'd expect when extending out to a new class.
        >
        What I want/would like to do is to include multiple scripts which
        extend the base class of FPDF. I've managed to daisy-chain these
        classes similar to:
        -------------------------------------------------------------------------
        <?php
        class FPDF {
        function FPDF(){ echo "class FPDF<BR>";}
        function setSize(){ echo "setSize_FPDF() <BR>";}
        }
        class B extends FPDF {
        function B(){ echo "class B<BR>";}
        function setSize(){ echo "setSize_B()<BR >......";parent ::setSize();}
        }
        class C extends B {
        function C(){ echo "class C<BR>";}
        function setLimit(){ echo "setLimit_C()<B R>";}
        }
        class D extends C {
        function D(){ echo "class D<BR>";}
        function PrintLine(){ echo "PrintLine_D()< BR>";}
        }
        class me extends D {
        function me(){ echo "class me<BR>";}
        function setSize()
        {
        echo "setSize_E()<BR >&nbsp;&nbsp;&n bsp;";
        //--- this only gets the class B function
        parent::setSize ();
        }
        }
        //--- test code
        $x = new me();
        $x->FPDF();
        $x->PrintLine();
        $x->setSize();
        >
        /* --- output
        class me
        class FPDF
        PrintLine_D()
        setSize_E()
        setSize_B()
        setSize_FPDF()
        */
        ?>
        -------------------------------------------------------------------------
        Now even *I* know that OOP was never intended to be like this. Is
        there a better/correct way?
        >
        In my code how would I reference the FPDF setSize() method?
        FYI: the setSize is not overriding the base class, it is an internal
        function to that class only.
        ---------------------------------------------------------------
        jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
        ---------------------------------------------------------------

        Comment

        • Jerry Stuckle

          #5
          Re: PHP5 and Classes

          ybakos wrote:
          I don't quite get what you're trying to accomplish (but I'm not that
          bright).
          >
          You should avoid deep inheritance heirarchies.
          >
          Or perhaps you shouldn't override the methods in this manner, since you
          are trying to achieve the ability to 'reach back' up the inheritance
          chain to call the 'Grandparent' method.
          >
          Not sure if this helps.
          >
          Cheers,
          Yong
          >
          >
          >
          Jeff North wrote:
          >

          Actually, Yong, I find "deep inheritance hierarchies" to be useful at
          times. Of course they've been complicated - but each class only needs
          to worry about its own implementation and the interface of its base
          class. It all depends on what you need.

          As for overriding the functions - that's one of the beauties of OO
          programming. You call setSize() in the lowest class, and it processes
          the request and passes it on to the parent. Of course, each setSize()
          should have the same purpose - just each one performs whatever work is
          needed on its own class.

          P.S. Please don't top post. This group uses bottom posting as a
          standard. Thanks.

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

          Comment

          • Jeff North

            #6
            Re: PHP5 and Classes

            On 12 Jul 2006 08:52:06 -0700, in comp.lang.php "ybakos"
            <spam@humanorie nted.com>
            <1152719526.834 808.93590@s13g2 000cwa.googlegr oups.comwrote:
            >| I don't quite get what you're trying to accomplish (but I'm not that
            >| bright).
            >|
            >| You should avoid deep inheritance heirarchies.
            >|
            >| Or perhaps you shouldn't override the methods in this manner, since you
            >| are trying to achieve the ability to 'reach back' up the inheritance
            >| chain to call the 'Grandparent' method.
            That's the whole problem, I do not want to override the base class.
            Each of these 'add-ons' have used the same function/method name in
            several places.
            >| Not sure if this helps.
            >|
            >| Cheers,
            >| Yong
            ---------------------------------------------------------------
            jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
            ---------------------------------------------------------------

            Comment

            Working...