Calling a subclass's method

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

    Calling a subclass's method

    Hello

    I'm new to OOP in PHP and I have this question:

    I have a class called "Form", which contains a collection of classes
    (objects) called "Textbox".
    Now I need to call the Textbox class's method "getOuterHTML() ".

    In Visual Basic, you should do it like this:
    oForm.getTextBo xByID("Number") .getOuterHTML()

    In PHP I tried this: $oForm->getTextBoxByID ("Number")->getOuterHTML() ;
    But that doesn't work...

    Is there a way to do this in one statement? (I use PHP4).


    Thanks in advance!
    Ramon.


  • Rik

    #2
    Re: Calling a subclass's method

    Ramon wrote:[color=blue]
    > Hello
    >
    > I'm new to OOP in PHP and I have this question:
    >
    > I have a class called "Form", which contains a collection of classes
    > (objects) called "Textbox".
    > Now I need to call the Textbox class's method "getOuterHTML() ".
    >
    > In Visual Basic, you should do it like this:
    > oForm.getTextBo xByID("Number") .getOuterHTML()
    >
    > In PHP I tried this: $oForm->getTextBoxByID ("Number")->getOuterHTML() ;
    > But that doesn't work...
    >
    > Is there a way to do this in one statement? (I use PHP4).[/color]

    How does you object "contain" the textboxes, and how have you defined the
    method getTextBoxByID?

    I'm no real OOP programmer, but in this case I'd:

    <?php

    class B{
    function display(){
    echo "works";
    }
    }

    class A{
    var $bs = array();
    function add(){
    $this->bs[] = new B;
    }
    }

    $a = new A;
    $a->add();

    $a->bs[0]->display();

    ?>

    Grtz,
    --
    Rik Wasmus


    Comment

    • dbunny
      New Member
      • Jun 2006
      • 5

      #3
      PHP 4 or 5? 5 supports a syntax similar to what you have above, but I'd need more info. I know that PHP5 doesn't allow calling a subclass method if it's not declared explicitly in th e parent class (usually as abstract, or public...) But that's probably not what you're problem is. PHP4 doesn't allow "linking" together objects like you're doing (but 5 does), and so:

      $box = $oForm->getTextBoxByID ("Number");
      $contents = $box->getOuterHTML() ;

      Does this work?

      (Oh - re-read you have PHP 4. Nope, gotta use two statements or upgrade to PHP5)
      Last edited by dbunny; Jun 14 '06, 08:22 AM. Reason: Duh

      Comment

      • Tony Marston

        #4
        Re: Calling a subclass's method

        What you are describing is not a subclass but a contained object. A subclass
        inherits from a superclass whereas you are instantiating an object from
        within another object. There is a difference.

        What you want is this...

        $oForm->getTextBoxBy ID->getOuterHTML(" Number");

        --
        Tony Marston
        This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL

        Build apps faster with Rapid Application Development using open-source RAD tools, modern RAD frameworks, and rapid application design methods.



        "Ramon" <itsramon@zonne t.nl> wrote in message
        news:448fc280$0 $31637$e4fe514c @news.xs4all.nl ...[color=blue]
        > Hello
        >
        > I'm new to OOP in PHP and I have this question:
        >
        > I have a class called "Form", which contains a collection of classes
        > (objects) called "Textbox".
        > Now I need to call the Textbox class's method "getOuterHTML() ".
        >
        > In Visual Basic, you should do it like this:
        > oForm.getTextBo xByID("Number") .getOuterHTML()
        >
        > In PHP I tried this: $oForm->getTextBoxByID ("Number")->getOuterHTML() ;
        > But that doesn't work...
        >
        > Is there a way to do this in one statement? (I use PHP4).
        >
        >
        > Thanks in advance!
        > Ramon.
        >[/color]


        Comment

        • Ramon

          #5
          Re: Calling a subclass's method

          Thanks Tony for your explanation,

          but I don't think I understand your solution. The getOuterHTML method
          expects no parameters. This is the error I get: "Call to a member function
          on a non-object". Did you mean that I have to adjust the getOuterHTML method
          in any way?

          This is my code (in short):

          <?

          class TextBox {
          var $m_sID;
          var $m_sName;
          var $m_sValue;

          // Class Constructor:
          function TextBox ($sID, $sName, $sValue) {
          $this->m_sID = $sID;
          $this->m_sName = $sName;
          $this->m_sValue = $sValue;
          }

          function getID() {
          return $this->m_sID;
          }

          function getOuterHTML () {
          return "<input type='text' name='" . $this->m_sName . "' value=' .
          $this->m_sValue . '>";
          {

          }

          class HTMLForm {
          var $m_oTextBoxes = array();

          function addTextBox($oTe xtBox) {
          $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
          }

          function getTextBoxByID ($sID) {
          return ($this->m_oTextBoxes[$sID]);
          }
          }

          $oTxtNumber = new TextBox ("Nr", "Number", "123");
          $oForm->addTextBox ($oTxtNumber);

          echo $oForm->getTextBoxByID ("Nr")->getOuterHTML() ;

          ?>


          "Tony Marston" <tony@NOSPAM.de mon.co.uk> wrote in message
          news:e6ogua$pti $1$8302bc10@new s.demon.co.uk.. .[color=blue]
          > What you are describing is not a subclass but a contained object. A
          > subclass inherits from a superclass whereas you are instantiating an
          > object from within another object. There is a difference.
          >
          > What you want is this...
          >
          > $oForm->getTextBoxBy ID->getOuterHTML(" Number");
          >
          > --
          > Tony Marston
          > http://www.tonymarston.net
          > http://www.radicore.org
          >
          >
          > "Ramon" <itsramon@zonne t.nl> wrote in message
          > news:448fc280$0 $31637$e4fe514c @news.xs4all.nl ...[color=green]
          >> Hello
          >>
          >> I'm new to OOP in PHP and I have this question:
          >>
          >> I have a class called "Form", which contains a collection of classes
          >> (objects) called "Textbox".
          >> Now I need to call the Textbox class's method "getOuterHTML() ".
          >>
          >> In Visual Basic, you should do it like this:
          >> oForm.getTextBo xByID("Number") .getOuterHTML()
          >>
          >> In PHP I tried this: $oForm->getTextBoxByID ("Number")->getOuterHTML() ;
          >> But that doesn't work...
          >>
          >> Is there a way to do this in one statement? (I use PHP4).
          >>
          >>
          >> Thanks in advance!
          >> Ramon.
          >>[/color]
          >
          >[/color]


          Comment

          • Rik

            #6
            Re: Calling a subclass's method

            Ramon wrote:[color=blue]
            > This is my code (in short):[/color]
            [color=blue]
            > function getOuterHTML () {
            > return "<input type='text' name='" . $this->m_sName . "'
            > value=' . $this->m_sValue . '>";
            > {[/color]


            I assume the { is } in your actual code?

            Try this:

            <?
            class TextBox {
            var $m_sID;
            var $m_sName;
            var $m_sValue;

            // Class Constructor:
            function TextBox ($sID, $sName, $sValue) {
            $this->m_sID = $sID;
            $this->m_sName = $sName;
            $this->m_sValue = $sValue;
            }

            function getID() {
            return $this->m_sID;
            }

            function getOuterHTML(){
            return "<input type='text' name='$this->m_sName'
            value='$this->m_sValue'>";
            }

            }

            class HTMLForm {
            var $m_oTextBoxes = array();

            function addTextBox($oTe xtBox) {
            $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
            }
            }

            $oTxtNumber = new TextBox ("Nr", "Number", "123");
            $oForm = new HTMLForm;
            $oForm->addTextBox($oT xtNumber);
            echo $oForm->m_oTextBoxes["Nr"]->getOuterHTML() ;
            ?>

            Works fine in PHP4 here.

            Grtz,
            --
            Rik Wasmus


            Comment

            • Ramon

              #7
              Re: Calling a subclass's method

              Thanks Rik,

              this does work.
              I'm sorry that I keep asking, but I'm trying to understand this.

              I wonder if this is the correct way to address the $m_oTextBoxes array.
              That array should be treated as if it were private, I suppose..?

              Actually, I should have an interface method to call this array:


              class HTMLForm {
              var $m_oTextBoxes = array();


              // Added method: =============== =============== ==========

              function getTextBoxByID( $sID) {
              return This->m_oTextBoxes[$sID];
              }

              // =============== =============== =============== =========

              function addTextBox($oTe xtBox) {
              $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
              }

              }

              Then, I still have the question how to call the right instance of the
              TextBox-class's getOuterHTML method.

              Because this will not work here: echo
              $oForm->m_oTextBoxes["Nr"]->getOuterHTML() ;


              Thanks for your patience!
              Ramon.

              ?>



              "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
              news:581f9$448f d547$8259c69c$1 4103@news1.tude lft.nl...[color=blue]
              > Ramon wrote:[color=green]
              >> This is my code (in short):[/color]
              >[color=green]
              >> function getOuterHTML () {
              >> return "<input type='text' name='" . $this->m_sName . "'
              >> value=' . $this->m_sValue . '>";
              >> {[/color]
              >
              >
              > I assume the { is } in your actual code?
              >
              > Try this:
              >
              > <?
              > class TextBox {
              > var $m_sID;
              > var $m_sName;
              > var $m_sValue;
              >
              > // Class Constructor:
              > function TextBox ($sID, $sName, $sValue) {
              > $this->m_sID = $sID;
              > $this->m_sName = $sName;
              > $this->m_sValue = $sValue;
              > }
              >
              > function getID() {
              > return $this->m_sID;
              > }
              >
              > function getOuterHTML(){
              > return "<input type='text' name='$this->m_sName'
              > value='$this->m_sValue'>";
              > }
              >
              > }
              >
              > class HTMLForm {
              > var $m_oTextBoxes = array();
              >
              > function addTextBox($oTe xtBox) {
              > $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
              > }
              > }
              >
              > $oTxtNumber = new TextBox ("Nr", "Number", "123");
              > $oForm = new HTMLForm;
              > $oForm->addTextBox($oT xtNumber);
              > echo $oForm->m_oTextBoxes["Nr"]->getOuterHTML() ;
              > ?>
              >
              > Works fine in PHP4 here.
              >
              > Grtz,
              > --
              > Rik Wasmus
              >
              >[/color]


              Comment

              • Rik

                #8
                Re: Calling a subclass's method

                Ramon wrote:[color=blue]
                > Thanks Rik,
                >
                > this does work.
                > I'm sorry that I keep asking, but I'm trying to understand this.
                >
                > I wonder if this is the correct way to address the $m_oTextBoxes
                > array. That array should be treated as if it were private, I
                > suppose..?[/color]

                In PHP4, it cannot be made private.
                An array in an object is just like any other array, so it's the correct way
                of adressing it.

                [color=blue]
                > Actually, I should have an interface method to call this array:[/color]

                I've been trying some things, it seems it cannot be done in one line, except
                when using constructs like:
                call_user_func( array($oForm->getgetTextBoxB yID("Nr"), 'outerHTML));

                But I'm no real OOP programmer, maybe someone else could enlighten you.
                [color=blue]
                > Then, I still have the question how to call the right instance of the
                > TextBox-class's getOuterHTML method.
                >
                > Because this will not work here: echo
                > $oForm->m_oTextBoxes["Nr"]->getOuterHTML() ;[/color]

                First it does work, now it doesn't work? Works perfectly here.

                If it actually doesn't work, try turning on error_reporting and var_dump()
                your objects. (For instance, in the example code you forgot to instantiate
                the $oForm object).

                If you mean it isn't suitable to your needs, perhaps some more detailed
                explanation of your exact needs are in order.

                Grtz,
                --
                Rik Wasmus


                Comment

                • Ramon

                  #9
                  Re: Calling a subclass's method

                  Ok,

                  thanks for your efforts Rik.
                  At least you gave me a way to make it work!

                  groetjes,
                  Ramon.



                  "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
                  news:ec31e$448f ddc7$8259c69c$1 5457@news1.tude lft.nl...[color=blue]
                  > Ramon wrote:[color=green]
                  >> Thanks Rik,
                  >>
                  >> this does work.
                  >> I'm sorry that I keep asking, but I'm trying to understand this.
                  >>
                  >> I wonder if this is the correct way to address the $m_oTextBoxes
                  >> array. That array should be treated as if it were private, I
                  >> suppose..?[/color]
                  >
                  > In PHP4, it cannot be made private.
                  > An array in an object is just like any other array, so it's the correct
                  > way
                  > of adressing it.
                  >
                  >[color=green]
                  >> Actually, I should have an interface method to call this array:[/color]
                  >
                  > I've been trying some things, it seems it cannot be done in one line,
                  > except
                  > when using constructs like:
                  > call_user_func( array($oForm->getgetTextBoxB yID("Nr"), 'outerHTML));
                  >
                  > But I'm no real OOP programmer, maybe someone else could enlighten you.
                  >[color=green]
                  >> Then, I still have the question how to call the right instance of the
                  >> TextBox-class's getOuterHTML method.
                  >>
                  >> Because this will not work here: echo
                  >> $oForm->m_oTextBoxes["Nr"]->getOuterHTML() ;[/color]
                  >
                  > First it does work, now it doesn't work? Works perfectly here.
                  >
                  > If it actually doesn't work, try turning on error_reporting and var_dump()
                  > your objects. (For instance, in the example code you forgot to instantiate
                  > the $oForm object).
                  >
                  > If you mean it isn't suitable to your needs, perhaps some more detailed
                  > explanation of your exact needs are in order.
                  >
                  > Grtz,
                  > --
                  > Rik Wasmus
                  >
                  >[/color]


                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Calling a subclass's method

                    Ramon wrote:[color=blue]
                    > Thanks Rik,
                    >
                    > this does work.
                    > I'm sorry that I keep asking, but I'm trying to understand this.
                    >
                    > I wonder if this is the correct way to address the $m_oTextBoxes array.
                    > That array should be treated as if it were private, I suppose..?
                    >
                    > Actually, I should have an interface method to call this array:
                    >
                    >
                    > class HTMLForm {
                    > var $m_oTextBoxes = array();
                    >
                    >
                    > // Added method: =============== =============== ==========
                    >
                    > function getTextBoxByID( $sID) {
                    > return This->m_oTextBoxes[$sID];
                    > }
                    >
                    > // =============== =============== =============== =========
                    >
                    > function addTextBox($oTe xtBox) {
                    > $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
                    > }
                    >
                    > }
                    >
                    > Then, I still have the question how to call the right instance of the
                    > TextBox-class's getOuterHTML method.
                    >
                    > Because this will not work here: echo
                    > $oForm->m_oTextBoxes["Nr"]->getOuterHTML() ;
                    >
                    >
                    > Thanks for your patience!
                    > Ramon.
                    >[/color]

                    Ramon,

                    Yes, you should treat the array as private, even in PHP 4 where you can't
                    declare it such. One of the purposes of OO is to hide implementation details -
                    such as your data members.

                    The following (slightly modified from your previous post) works for me on PHP 5.1.2:

                    <?

                    class TextBox {
                    var $m_sID;
                    var $m_sName;
                    var $m_sValue;

                    // Class Constructor:
                    function TextBox ($sID, $sName, $sValue) {
                    $this->m_sID = $sID;
                    $this->m_sName = $sName;
                    $this->m_sValue = $sValue;
                    }

                    function getID() {
                    return $this->m_sID;
                    }

                    function getOuterHTML () {
                    return "<input type='text' name='" . $this->m_sName . "' value=' .
                    $this->m_sValue . '>";
                    }

                    }

                    class HTMLForm {
                    var $m_oTextBoxes = array();

                    function addTextBox($oTe xtBox) {
                    $this->m_oTextBoxes[$oTextBox->getID()] = $oTextBox;
                    }

                    function getTextBoxByID ($sID) {
                    return ($this->m_oTextBoxes[$sID]);
                    }
                    }

                    $oForm= new HTMLForm();
                    $oTxtNumber = new TextBox ("Nr", "Number", "123");
                    $oForm->addTextBox ($oTxtNumber);

                    echo $oForm->getTextBoxByID ("Nr")->getOuterHTML() ;

                    ?>




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

                    Comment

                    • Rik

                      #11
                      Re: Calling a subclass's method

                      Jerry Stuckle wrote:[color=blue]
                      > Yes, you should treat the array as private, even in PHP 4 where you
                      > can't
                      > declare it such. One of the purposes of OO is to hide implementation
                      > details - such as your data members.
                      >
                      > The following (slightly modified from your previous post) works for
                      > me on PHP 5.1.2:
                      >
                      > <snip code>[/color]

                      That's what I tried, but PHP4 has thsi to say about thes second ->:
                      Parse error: syntax error, unexpected T_OBJECT_OPERAT OR, expecting ',' or
                      ';'

                      PHP5 indeed handles this with no problems.

                      Maybe this has something to do with returning by reference?

                      Grtz,
                      --
                      Rik Wasmus


                      Comment

                      • Markus Ernst

                        #12
                        Re: Calling a subclass's method

                        Rik schrieb:[color=blue]
                        > That's what I tried, but PHP4 has thsi to say about thes second ->:
                        > Parse error: syntax error, unexpected T_OBJECT_OPERAT OR, expecting ',' or
                        > ';'
                        >
                        > PHP5 indeed handles this with no problems.
                        >
                        > Maybe this has something to do with returning by reference?[/color]

                        Indeed PHP4 passes objects by value (thus creates new instances), which
                        has been changed in PHP5. To pass references in PHP4 use the & character:

                        // & in function declaration to return reference
                        function &getTextBoxByID ($sID) {
                        return $this->m_oTextBoxes[$sID];
                        }

                        // & before parameter to pass it by reference
                        // & with = to assign reference
                        function addTextBox(&$oT extBox) {
                        $this->m_oTextBoxes[$oTextBox->getID()] =& $oTextBox;
                        }

                        HTH
                        Markus

                        Comment

                        • Jerry Stuckle

                          #13
                          Re: Calling a subclass's method

                          Rik wrote:[color=blue]
                          > Jerry Stuckle wrote:
                          >[color=green]
                          >>Yes, you should treat the array as private, even in PHP 4 where you
                          >>can't
                          >>declare it such. One of the purposes of OO is to hide implementation
                          >>details - such as your data members.
                          >>
                          >>The following (slightly modified from your previous post) works for
                          >>me on PHP 5.1.2:
                          >>
                          >><snip code>[/color]
                          >
                          >
                          > That's what I tried, but PHP4 has thsi to say about thes second ->:
                          > Parse error: syntax error, unexpected T_OBJECT_OPERAT OR, expecting ',' or
                          > ';'
                          >
                          > PHP5 indeed handles this with no problems.
                          >
                          > Maybe this has something to do with returning by reference?
                          >
                          > Grtz,[/color]

                          Don't know - haven't used PHP 4 for ages. But as Markus pointed out, you can
                          return the object by reference.

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

                          Comment

                          Working...