Fatal Error on Polymorphism

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

    #1

    Fatal Error on Polymorphism

    I need polymorphism in php.
    When I run this following code I get the following error.
    Fatal error: Cannot redeclare aClass::aPrint( ) in C:\Program Files
    \xampp\htdocs\k 7\prCls.php on line 16

    How I rectify it?


    <?php

    class aClass{

    function aPrint($name){
    echo $name;
    }

    function aPrint($name,$a ge){
    echo $name;
    echo $age;
    }

    }

    $myClass = new aClass;
    $myClass->aPrint('Kesava n');

    $myClass2 = new aClass;
    $myClass2->aPrint('Kesava n',24);
    ?>

    /
    *************** *************** *************** *************** *************** *************** **************/
    Kesavan M
    m.kesavan@hotma il.com

  • Michael Fesser

    #2
    Re: Fatal Error on Polymorphism

    ..oO(Kesavan)
    >I need polymorphism in php.
    >When I run this following code I get the following error.
    >Fatal error: Cannot redeclare aClass::aPrint( ) in C:\Program Files
    >\xampp\htdocs\ k7\prCls.php on line 16
    >
    >How I rectify it?
    >
    >
    ><?php
    >
    > class aClass{
    >
    > function aPrint($name){
    > echo $name;
    > }
    >
    > function aPrint($name,$a ge){
    > echo $name;
    > echo $age;
    > }
    >[...]
    PHP doesn't support that. But you could make $age an optional argument
    and check inside the function whether it was passed or not:

    function aPrint($name, $age = NULL) {
    print $name;
    print !is_null($age) ? $age : '';
    }

    Micha

    Comment

    • Kesavan

      #3
      Re: Fatal Error on Polymorphism

      On Mar 1, 11:47 am, Michael Fesser <neti...@gmx.de wrote:
      .oO(Kesavan)
      >
      >
      >
      I need polymorphism in php.
      When I run this following code I get the following error.
      Fatal error: Cannot redeclare aClass::aPrint( ) in C:\Program Files
      \xampp\htdocs\k 7\prCls.php on line 16
      >
      How I rectify it?
      >
      <?php
      >
      class aClass{
      >
      function aPrint($name){
      echo $name;
      }
      >
      function aPrint($name,$a ge){
      echo $name;
      echo $age;
      }
      [...]
      >
      PHP doesn't support that. But you could make $age an optional argument
      and check inside the function whether it was passed or not:
      >
      function aPrint($name, $age = NULL) {
      print $name;
      print !is_null($age) ? $age : '';
      }
      >
      Micha
      Thanks Michael,

      Your trick help's me..,
      Isn't PHP fully OOP language/

      Kesavan M
      m.kesavan@hotma il.com

      Comment

      • Erwin Moller

        #4
        Re: Fatal Error on Polymorphism

        Kesavan wrote:
        On Mar 1, 11:47 am, Michael Fesser <neti...@gmx.de wrote:
        >.oO(Kesavan)
        >>
        >>
        >>
        >I need polymorphism in php.
        >When I run this following code I get the following error.
        >Fatal error: Cannot redeclare aClass::aPrint( ) in C:\Program Files
        >\xampp\htdocs\ k7\prCls.php on line 16
        >>
        >How I rectify it?
        >>
        ><?php
        >>
        class aClass{
        >>
        function aPrint($name){
        echo $name;
        }
        >>
        function aPrint($name,$a ge){
        echo $name;
        echo $age;
        }
        >[...]
        >>
        >PHP doesn't support that. But you could make $age an optional argument
        >and check inside the function whether it was passed or not:
        >>
        > function aPrint($name, $age = NULL) {
        > print $name;
        > print !is_null($age) ? $age : '';
        > }
        >>
        >Micha
        >
        Thanks Michael,
        >
        Your trick help's me..,
        Isn't PHP fully OOP language/
        No, it isn't: go Java or C++ or something like that if you want 'real'
        method overloading.
        The support PHP gives for OOP is enough to get almost all tasks done.


        Regards,
        Erwin Moller

        Comment

        • Michael Fesser

          #5
          Re: Fatal Error on Polymorphism

          ..oO(Kesavan)
          >Your trick help's me..,
          >Isn't PHP fully OOP language/
          Method overloading is not necessarily a feature required to be an OOP
          language. Usually you will find that feature in statically-typed
          languages, where the compiler can decide which function to call by
          looking at the types of the passed arguments. In dynamically-typed
          languages it's much more difficult, if not impossible. I don't know if
          there's any scripting language at all that supports that.

          Micha

          Comment

          Working...