Polymorphism in PHP

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

    Polymorphism in PHP

    Hello! I am working with PHP and MySQL in a proyect with some partners
    of our university, and we are thinking about building some classes and
    use inheritance and polymorphism.

    The problem is that we are not sure of PHP implementing polymorphism
    and we couldn't find info about it. I will really appreciate if
    someone could give us some recommendations about some books to read
    about it and/or pages to look for some info.

    Thanks for all.

    PD: I am from Argentina, so my real language is Spanish. I am
    effective reading in English, but my writing has to be improved. =P
  • Jochen Daum

    #2
    Re: Polymorphism in PHP

    Hi,

    On 20 Apr 2004 18:42:55 -0700, gastonkeller@ho tmail.com (Gurtaj)
    wrote:
    [color=blue]
    >Hello! I am working with PHP and MySQL in a proyect with some partners
    >of our university, and we are thinking about building some classes and
    >use inheritance and polymorphism.
    >
    >The problem is that we are not sure of PHP implementing polymorphism
    >and we couldn't find info about it. I will really appreciate if
    >someone could give us some recommendations about some books to read
    >about it and/or pages to look for some info.[/color]

    Try www.php.net, it explains how the classes work quite well.

    Example Polymorphism:

    class doer{

    function do();

    }

    class do_b extends doer{

    function do(){
    print "I'm doing b";
    }
    }

    class do_c extends doer{

    function do(){
    print "I'm doing c";
    }
    }


    $sample_doer = get_a_sample_do er_somehow();

    $sample_doer->do();

    does either nothing (class doer), "I'm doing b", "I'm doing c",
    depending on what you instantiated.

    What PHP4 doesn;t have is declarations like abstract, public, private,
    you have to cover that by proper coding and debugging mechanisms.

    Also redefining constructors doesn;t work, which is a bit of a pain.

    HTH, Jochen








    [color=blue]
    >
    >Thanks for all.
    >
    >PD: I am from Argentina, so my real language is Spanish. I am
    >effective reading in English, but my writing has to be improved. =P[/color]

    --
    Jochen Daum - Cabletalk Group Ltd.
    PHP DB Edit Toolkit -- PHP scripts for building
    database editing interfaces.
    Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

    Comment

    • Berislav Lopac

      #3
      Re: Polymorphism in PHP

      Jochen Daum wrote:[color=blue]
      > Also redefining constructors doesn;t work, which is a bit of a pain.[/color]

      What do you mean by that? You can define constructor for a class, and then
      another for a subclass, if that's what you meant.

      Berislav

      --
      If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
      Groucho, Chico, and Harpo, then Usenet is Zeppo.


      Comment

      • Tony Marston

        #4
        Re: Polymorphism in PHP


        "Gurtaj" <gastonkeller@h otmail.com> wrote in message
        news:579f9938.0 404201742.57a2b 18f@posting.goo gle.com...[color=blue]
        > Hello! I am working with PHP and MySQL in a proyect with some partners
        > of our university, and we are thinking about building some classes and
        > use inheritance and polymorphism.[/color]

        PHP 4 has functions that allow for the creation of classes with properties
        amd methods, subclassing etc so there should not be a problem. There is no
        such thing as a particular function or command that allows for polymorhism -
        it is all in the way you write your code.

        Take a look at http://www.tonymarston.co.uk/php-mys...seobjects.html
        and http://www.tonymarston.co.uk/php-mys...eobjects2.html which
        describe how I have implemented a table class which I use to access all my
        database tables.

        There is also http://www.tonymarston.co.uk/php-mys...d-bad-oop.html which
        handles some of the criticism of my approach.

        --
        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



        [color=blue]
        > The problem is that we are not sure of PHP implementing polymorphism
        > and we couldn't find info about it. I will really appreciate if
        > someone could give us some recommendations about some books to read
        > about it and/or pages to look for some info.
        >
        > Thanks for all.
        >
        > PD: I am from Argentina, so my real language is Spanish. I am
        > effective reading in English, but my writing has to be improved. =P[/color]


        Comment

        • Chung Leong

          #5
          Re: Polymorphism in PHP


          "Gurtaj" <gastonkeller@h otmail.com> wrote in message
          news:579f9938.0 404201742.57a2b 18f@posting.goo gle.com...[color=blue]
          > Hello! I am working with PHP and MySQL in a proyect with some partners
          > of our university, and we are thinking about building some classes and
          > use inheritance and polymorphism.[/color]

          OK, I see you have the solution. But what is it that you're trying to solve?

          Imagine walking into a doctor's office and being greeted by "we're
          thininking about a triple bypass operation and a few electroshock sessions."


          Comment

          • Jochen Daum

            #6
            Re: Polymorphism in PHP

            Hi,

            On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
            <berislav.lopac @dimedia.hr> wrote:
            [color=blue]
            >Jochen Daum wrote:[color=green]
            >> Also redefining constructors doesn;t work, which is a bit of a pain.[/color]
            >
            >What do you mean by that? You can define constructor for a class, and then
            >another for a subclass, if that's what you meant.[/color]

            Should have made myself more clear.

            What doesn't work, is that you cannot return a subclass object from a
            constructor.

            I wanted to use that to implement a Strategy pattern (see Book Design
            Patterns by Booch etc), which goes roughly like that:

            class shape{

            function shape($type){

            switch ($type){
            case "circle":
            $x = new circle();
            break;
            ...
            }
            return $x;
            }

            function draw(){
            print "abstract here";
            }
            }

            Assuming there are different shapes as subclasses of shape.

            Shape does always return a class shape and will always use the draw
            function of shape, if you go:

            $s = new shape("circle") ;
            $s->draw();


            HTH, Jochen[color=blue]
            >
            >Berislav[/color]

            --
            Jochen Daum - Cabletalk Group Ltd.
            PHP DB Edit Toolkit -- PHP scripts for building
            database editing interfaces.
            Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

            Comment

            • nospam@geniegate.com

              #7
              Re: Polymorphism in PHP

              Jochen Daum <jochen.daum@ca bletalk.co.nz> wrote:[color=blue]
              > What doesn't work, is that you cannot return a subclass object from a
              > constructor.
              >
              > I wanted to use that to implement a Strategy pattern (see Book Design
              > Patterns by Booch etc), which goes roughly like that:
              >
              > class shape{
              >
              > function shape($type){
              >
              > switch ($type){
              > case "circle":
              > $x = new circle();
              > break;
              > ...
              > }
              > return $x;
              > }
              >
              > function draw(){
              > print "abstract here";
              > }
              > }[/color]

              I think you can "kind of" get that type of functionality via a so-called
              "object factory". (That would could have the advantage of loading a PHP
              document implementing the desired class on demand) Unfortunately it's
              not a clean subclass.

              Jamie


              --
              http://www.geniegate.com Custom web programming
              User Management Solutions Perl / PHP / Java / UNIX

              Comment

              • nospam@geniegate.com

                #8
                Re: Polymorphism in PHP

                Gurtaj <gastonkeller@h otmail.com> wrote:[color=blue]
                > Hello! I am working with PHP and MySQL in a proyect with some partners
                > of our university, and we are thinking about building some classes and
                > use inheritance and polymorphism.
                >
                > The problem is that we are not sure of PHP implementing polymorphism
                > and we couldn't find info about it. I will really appreciate if
                > someone could give us some recommendations about some books to read
                > about it and/or pages to look for some info.
                >
                > Thanks for all.
                >
                > PD: I am from Argentina, so my real language is Spanish. I am
                > effective reading in English, but my writing has to be improved. =P[/color]

                As far as I can tell, PHP really doesn't have that feature:

                // This won't work.
                function myFunction(long a){ ... }
                function myFunction(floa t a) { ... }

                This is a side effect of loosely typed languages. To implement similiar
                functionality you'd probably need to write PHP code to determine if $a
                is a long or a float, then do the right thing.

                The extends and overriding methods DOES work though. PHP's version
                of java's 'super' is 'parent'.

                I've got a class file at:



                Specifically designed to be used in this manner. (Override the
                interesting methods to do whatever you want)

                Jamie

                --
                http://www.geniegate.com Custom web programming
                User Management Solutions Perl / PHP / Java / UNIX

                Comment

                • Phil Roberts

                  #9
                  Re: Polymorphism in PHP

                  With total disregard for any kind of safety measures
                  nospam@geniegat e.com leapt forth and uttered:
                  [color=blue]
                  > As far as I can tell, PHP really doesn't have that feature:
                  >[/color]

                  Polymorphism isn't a "feature", it's a result of code design. And
                  static typing is *not* a dependancy of polymorphism. Many languages
                  with much greater object-orientation than PHP use dynamic typing.
                  (Smalltalk anyone?)
                  [color=blue]
                  > // This won't work.
                  > function myFunction(long a){ ... }
                  > function myFunction(floa t a) { ... }[/color]

                  Thats won't work because what you're describing is not polymorphism
                  but "object overloading". Whereby the type passed to a method
                  determines the method used. This is largely unnecessary in PHP as
                  PHP allows default parameter values wheras Java does not.

                  Personally I've never come across a single scenario where I thought
                  to myself "I wish I could use method overloading here".


                  --
                  Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/

                  Comment

                  • Gurtaj

                    #10
                    Re: Polymorphism in PHP

                    "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<HdCdnb8w3 PJajRrdRVn-jA@comcast.com> ...
                    [color=blue]
                    > OK, I see you have the solution. But what is it that you're trying to solve?
                    >
                    > Imagine walking into a doctor's office and being greeted by "we're
                    > thininking about a triple bypass operation and a few electroshock sessions."[/color]

                    What we were thinking was to implement a classes hierarchy that
                    allowed us to use just one operation (defined in the ancestor) to save
                    any class into the data base. Here is where polymorphism would get in
                    the game.

                    But we were thinking about it and decided yesterday to implement the
                    classes in the hierarchy with an operation that allow the object save
                    itself into the data base.

                    One more time, thanks for the help and excuse my english.

                    Comment

                    • Joshua Beall

                      #11
                      Re: Polymorphism in PHP

                      "Jochen Daum" <jochen.daum@ca bletalk.co.nz> wrote in message
                      news:qh4e8018g2 fnbclkh702u3grg kkru6sgos@4ax.c om...[color=blue]
                      > Hi,
                      >
                      > On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
                      > <berislav.lopac @dimedia.hr> wrote:
                      >[color=green]
                      > >Jochen Daum wrote:[color=darkred]
                      > >> Also redefining constructors doesn;t work, which is a bit of a pain.[/color]
                      > >
                      > >What do you mean by that? You can define constructor for a class, and[/color][/color]
                      then[color=blue][color=green]
                      > >another for a subclass, if that's what you meant.[/color]
                      >
                      > Should have made myself more clear.
                      >
                      > What doesn't work, is that you cannot return a subclass object from a
                      > constructor.[/color]

                      Sure ya can. I do it all the time :-)

                      Watcha this...

                      class shape{

                      function shape($type){

                      switch ($type){
                      case "circle":
                      $this = new circle();
                      break;
                      ...
                      }
                      return;
                      }

                      Ok, ok, I did not technically "return" the value - but it accomplishes what
                      you wanted to do.


                      Comment

                      • Chung Leong

                        #12
                        Re: Polymorphism in PHP

                        "Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message
                        news:wsPhc.4425 6$L31.20367@nwr ddc01.gnilink.n et...[color=blue]
                        > "Jochen Daum" <jochen.daum@ca bletalk.co.nz> wrote in message
                        > news:qh4e8018g2 fnbclkh702u3grg kkru6sgos@4ax.c om...[color=green]
                        > > Hi,
                        > >
                        > > On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
                        > > <berislav.lopac @dimedia.hr> wrote:
                        > >[color=darkred]
                        > > >Jochen Daum wrote:
                        > > >> Also redefining constructors doesn;t work, which is a bit of a pain.
                        > > >
                        > > >What do you mean by that? You can define constructor for a class, and[/color][/color]
                        > then[color=green][color=darkred]
                        > > >another for a subclass, if that's what you meant.[/color]
                        > >
                        > > Should have made myself more clear.
                        > >
                        > > What doesn't work, is that you cannot return a subclass object from a
                        > > constructor.[/color]
                        >
                        > Sure ya can. I do it all the time :-)
                        >
                        > Watcha this...
                        >
                        > class shape{
                        >
                        > function shape($type){
                        >
                        > switch ($type){
                        > case "circle":
                        > $this = new circle();
                        > break;
                        > ...
                        > }
                        > return;
                        > }
                        >
                        > Ok, ok, I did not technically "return" the value - but it accomplishes[/color]
                        what[color=blue]
                        > you wanted to do.
                        >
                        >[/color]

                        Why not just do $object = new $shape(); ? It's perfectly clear what you're
                        trying to accomplish. Bid and switch constructors are immoral if you ask me
                        :-)


                        Comment

                        • Jochen Daum

                          #13
                          Re: Polymorphism in PHP

                          Hi Chung,

                          On Thu, 22 Apr 2004 19:21:32 -0400, "Chung Leong"
                          <chernyshevsky@ hotmail.com> wrote:
                          [color=blue]
                          >"Joshua Beall" <jbeall@donotsp am.remove.me.he raldic.us> wrote in message
                          >news:wsPhc.442 56$L31.20367@nw rddc01.gnilink. net...[color=green]
                          >> "Jochen Daum" <jochen.daum@ca bletalk.co.nz> wrote in message
                          >> news:qh4e8018g2 fnbclkh702u3grg kkru6sgos@4ax.c om...[color=darkred]
                          >> > Hi,
                          >> >
                          >> > On Wed, 21 Apr 2004 08:20:41 +0200, "Berislav Lopac"
                          >> > <berislav.lopac @dimedia.hr> wrote:
                          >> >
                          >> > >Jochen Daum wrote:
                          >> > >> Also redefining constructors doesn;t work, which is a bit of a pain.
                          >> > >
                          >> > >What do you mean by that? You can define constructor for a class, and[/color]
                          >> then[color=darkred]
                          >> > >another for a subclass, if that's what you meant.
                          >> >
                          >> > Should have made myself more clear.
                          >> >
                          >> > What doesn't work, is that you cannot return a subclass object from a
                          >> > constructor.[/color]
                          >>
                          >> Sure ya can. I do it all the time :-)
                          >>
                          >> Watcha this...
                          >>
                          >> class shape{
                          >>
                          >> function shape($type){
                          >>
                          >> switch ($type){
                          >> case "circle":
                          >> $this = new circle();
                          >> break;
                          >> ...
                          >> }
                          >> return;
                          >> }
                          >>
                          >> Ok, ok, I did not technically "return" the value - but it accomplishes[/color]
                          >what[color=green]
                          >> you wanted to do.
                          >>
                          >>[/color]
                          >
                          >Why not just do $object = new $shape(); ? It's perfectly clear what you're
                          >trying to accomplish. Bid and switch constructors are immoral if you ask me[/color]


                          With a strategy pattern you want to "delegate" the decision of the
                          switch. Of course you can use an array instead or whatever.

                          HTH, Jochen[color=blue]
                          >:-)
                          >[/color]

                          --
                          Jochen Daum - Cabletalk Group Ltd.
                          PHP DB Edit Toolkit -- PHP scripts for building
                          database editing interfaces.
                          Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

                          Comment

                          • Oliver Grätz

                            #14
                            Re: Polymorphism in PHP

                            Phil Roberts schrieb:
                            [color=blue]
                            > Thats won't work because what you're describing is not polymorphism
                            > but "object overloading". Whereby the type passed to a method
                            > determines the method used. This is largely unnecessary in PHP as
                            > PHP allows default parameter values wheras Java does not.
                            >
                            > Personally I've never come across a single scenario where I thought
                            > to myself "I wish I could use method overloading here".[/color]

                            PHP doesn't support default parameters for reference type parameters. I
                            wanted to do this

                            function huba(&$relay=no ne)
                            {
                            //...
                            if($relay=none)
                            {
                            // special code if no relay object specified
                            }
                            //...
                            }

                            This is not possible. You MUST specify reference type parameters.


                            AllOLLi


                            PS: Wikipedia:
                            In computer science, polymorphism is the idea of allowing the same code
                            to be used with different types, resulting in more general and abstract
                            implementations .

                            Comment

                            Working...