Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question PHP Polymorphism

    Hello People,



    Can someone please give me an example of Polymorphism in PHP with a short code?



    Thanx

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP Code:
    abstract class Shape
    {
        public abstract function 
    calculateArea();
    }

    class 
    Rectangle extends Shape
    {
        public function 
    calculateArea()
        {
            return 
    $this->$this->y;
        }
    }

    class 
    Circle extends Shape
    {
        public function 
    calculateArea()
        {
            return 
    $this->$this->3.14;
        }

    That help?

    Edit:
    I should note that in PHP the polymorphism isn't quite the way it should be. I mean that it does work, but since we have a weak datatype, its not correct. In java or C#, the above could work like so:
    Code:
    Shape circle = new Circle(5);
    Shape square = new Rectangle(5, 5);
    
    circle.calculateArea();
    square.calculateArea();
    While it still works in PHP, the idea is that circle and square are types of Shape, while in PHP there is no such way to declare this (except in typehinting for you're methods).
    Last edited by Fou-Lu; 03-12-2009 at 08:34 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    phantom007 (03-12-2009)

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Hi

    I am sure it will help if you can plz explain me what is happening and also why are you using an abstract class in a polymorphism example?


    Thanx

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    I am sure it will help if you can plz explain me what is happening and also why are you using an abstract class in a polymorphism example?
    Fou-Lu had given a nice explanation for what's an abstract class
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    phantom007 (03-12-2009)

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    abstract class Shape
    {
        public abstract function 
    calculateArea();
    }

    class 
    Rectangle extends Shape
    {
        public function 
    calculateArea()
        {
            return 
    $this->$this->y;
        }
    }

    class 
    Circle extends Shape
    {
        public function 
    calculateArea()
        {
            return 
    $this->$this->3.14;
        }

    That help?

    Edit:
    I should note that in PHP the polymorphism isn't quite the way it should be. I mean that it does work, but since we have a weak datatype, its not correct. In java or C#, the above could work like so:
    Code:
    Shape circle = new Circle(5);
    Shape square = new Rectangle(5, 5);
    
    circle.calculateArea();
    square.calculateArea();
    While it still works in PHP, the idea is that circle and square are types of Shape, while in PHP there is no such way to declare this (except in typehinting for you're methods).
    I'm not sure, but I guess this is huppend because constructor and destructor are not inherited by default. I don't have much time to spare, but I'm curios, if this is happend if you write your own constructor for each extended class.

    best regards

  • Users who have thanked oesxyl for this post:

    phantom007 (03-13-2009)

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by oesxyl View Post
    I'm not sure, but I guess this is huppend because constructor and destructor are not inherited by default. I don't have much time to spare, but I'm curios, if this is happend if you write your own constructor for each extended class.

    best regards
    I'm not quite sure that I'm following you? The constructor / destructor is inherited in PHP (with a valid scope of course), though if you override them in child classes you have to explicitly call the parent constructor (this is something I do not like, I wish it would at least force the parent call). I guess thats probably due to the lack of overloading capability in PHP.
    I'm thinking that you're looking at the edit part of the code. I'm not too worried about the constructor definitions on those, but the important part was the storing the Circle and Rectangle object's as shapes. In PHP, we don't have that luxury (which is what I mean by we don't really have polymorphism, since we don't really bind an inherited method to a Shape).
    PHP Code:
    $circle = new Circle(5);
    $rect = new Rectangle(55); 
    While in PHP these are extensions of Shape so can be validated against $circle instanceof Shape;, they are actually a Circle and Rectangle object, not a shape object (unlike the C#/Java ones shown earlier). However, this will work:
    PHP Code:
    function getArea(Shape $shape)
    {
         return 
    $shape->getArea();
    }

    $area getArea($circle); 
    which is a simple example of the typehinting. Thats about the extent of the polymorphism in PHP.

    Does that sorta clear up what I was trying to show? So yeah, we have polymorphism, but it doesn't appear to be 'real' polymorphism.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    phantom007 (03-13-2009)

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    maybe I don't understand what you mean by real pholimorphism. I see what you say more as week vs. strong type future/limitation of the language.
    As far as you can't declare a given type for a variable like Shape $rect, it must at first look to be of type of the constructor. Latter if you want to check for inheritance, to be sure that you have a common base class, you have another mechanism to do that. For example:
    PHP Code:
    class SpecialRectangle extends Rectangle {}

    $spec = new SpecialRectangle(3.0,4.0);
    echo 
    get_class($spec);
    echo 
    get_parent_class($spec);
    echo 
    get_parent_class(get_parent_class($spec)); 
    or you can use is_subclass_of(instance, classname) for checking.

    PS: you are right about constructor/destructor inheritance. I missunderstand what you said, wrong in what I said and also I'm not sure I understand you this time,

    best regards

  • Users who have thanked oesxyl for this post:

    phantom007 (03-13-2009)

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    We're on the same point, but looking at it from different sides. It really is just PHP's datatype weakness.
    Lets see if I can do up a better example of what I mean:
    Code:
    public class Circle extends Shape
    {
        public double getPerimiter()
        {
            return 2 * this.r * Math.PI;
        }
    }
    
    // Some main:
    Shape c = new Circle(5); // Memory would list this as typeof Shape
    c.getPerimiter(); // Illegal, no signatured getPerimiter in Shape
    ((Circle)c).getPerimeter(); // Legal, c is now a Circle
    PHP Code:
    class Circle extends Shape
    {
        public function 
    getPerimiter()
        {
            return 
    $this->M_PI;
        }
    }

    $c = new Circle(5); // Memory lists this as typeof Circle
    $c->getPerimiter(); // Legal, since $c is a Circle, not a Shape 
    So I guess what I mean as 'real' polymorphism is how its existing in memory. Strong datatype language (C#, Java, C++, etc) would indicate in memory that this 'Circle' is a 'Shape', while in PHP since it is weak has no way in memory to define it as anything but a 'Circle'. Even with the typehinting, it doesn't constraint the availability of non-existant function calls. So, if you typehinted the Shape class in a function, you can still call the getPerimiter on the shape (even though you shouldn't be able to), and if you passed it a Square instead of a Circle, it would toss a fatal error.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    I think I get it now,
    yes it's true what you say. I think is more a problem of habits about what level of checking you expect from a language. Take for example javascript, where language is a mushup of oop and functional futures. If you try to implement same level of checking like in a c++-like language, I'm sure you lose a lot of future of the language.

    best regards

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Exactly, thats a good way of putting it. And with that, you can see we were indeed talking about the same thing (just looking at it from different perspectives). Its all about the expectations ;D
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •