upcasting in PHP OOP

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

    upcasting in PHP OOP

    Lets suppose I have an class called DatastoreSelect . It has a method
    called getNextRow(). This method is not abstract, it is fully
    implemented. It also has a method called setInfoToBeSoug ht, which is
    abstract in the sense that it has no implementation.

    Now suppose I have an class called GetAllComments, which inherits from
    DatastoreSelect . GetAllComments does not have a method called
    getNextRow(). GetAllComments has a method called setInfoToBeSoug ht,
    which contains some SQL, like this:

    $query = "SELECT * FROM content WHERE type='comments' AND
    page=$pageId";

    GetAllComments also has a method called getInfo() which gets runs the
    query and gets a resource pointer.

    Can I do this:

    $gac = new GetAllComments( );
    $gac->setInfoToBeSou ght($pageId);
    $gac->getInfo();
    $datastoreRetur n = $gac->getNextRow() ;


    I mean, can I act like the method getNextRow() is there, even though
    it's implementation is in the parent class?

    If not, how else does one use the methods in super classes? Is there a
    way to do Java-style upcasting?
  • warstar

    #2
    Re: upcasting in PHP OOP

    On 17 Nov 2003 12:32:28 -0800, lkrubner@geocit ies.com (lawrence)
    wrote:
    [color=blue]
    >Lets suppose I have an class called DatastoreSelect . It has a method
    >called getNextRow(). This method is not abstract, it is fully
    >implemented. It also has a method called setInfoToBeSoug ht, which is
    >abstract in the sense that it has no implementation.
    >
    >Now suppose I have an class called GetAllComments, which inherits from
    >DatastoreSelec t. GetAllComments does not have a method called
    >getNextRow() . GetAllComments has a method called setInfoToBeSoug ht,
    >which contains some SQL, like this:
    >
    >$query = "SELECT * FROM content WHERE type='comments' AND
    >page=$pageId ";
    >
    >GetAllCommen ts also has a method called getInfo() which gets runs the
    >query and gets a resource pointer.
    >
    >Can I do this:
    >
    >$gac = new GetAllComments( );
    >$gac->setInfoToBeSou ght($pageId);
    >$gac->getInfo();
    >$datastoreRetu rn = $gac->getNextRow() ;
    >
    >
    >I mean, can I act like the method getNextRow() is there, even though
    >it's implementation is in the parent class?[/color]
    Yeah u can[color=blue]
    >
    >If not, how else does one use the methods in super classes? Is there a
    >way to do Java-style upcasting?[/color]

    Yeah u can

    Comment

    • Pedro Graca

      #3
      Re: upcasting in PHP OOP

      lawrence wrote:[color=blue]
      > Lets suppose I have an class called DatastoreSelect . It has a method
      > called getNextRow(). [...]
      >
      > Can I do this:
      >
      > $gac = new GetAllComments( );
      > $gac->setInfoToBeSou ght($pageId);
      > $gac->getInfo();
      > $datastoreRetur n = $gac->getNextRow() ;
      >
      >
      > I mean, can I act like the method getNextRow() is there, even though
      > it's implementation is in the parent class?
      >
      > If not, how else does one use the methods in super classes? Is there a
      > way to do Java-style upcasting?[/color]

      What happenned when you tried?

      When I tried this

      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      <?php
      class x1 {
      function out() {echo 'x1::out';}
      }

      class x2 extends x1 {
      function out2() {echo 'x2::out';}
      }

      $z = new x2;

      $z->out();
      echo "\n\n";
      $z->out2();
      echo "\n\n";
      ?>
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=


      I got this back

      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      x1::out

      x2::out

      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      --
      ..sig

      Comment

      Working...