Php 5 : polymorphic class

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

    Php 5 : polymorphic class

    Hello,


    With php4 I did the following in class initialisation :

    class base_class {

    function modify_polymorp hic($otherclass ){
    $this=new $otherclass();
    }

    function other_function( ){
    echo "Base method";
    }

    }

    class herited_class extends base_class {

    function other_function( ){
    echo "Herited method";
    }

    }

    $base=new base_class();
    $base->modify_polymor phic("herited_c lass");
    $base->other_function ();

    This will echo "Herited method" in PHP 4.
    But in PHP 5 it does not wand to work at all.
    PHP interpreter says :

    Fatal error: Cannot re-assign $this in scriptname.php on line 10
  • Jerry Stuckle

    #2
    Re: Php 5 : polymorphic class

    Baptiste Pillot wrote:[color=blue]
    > Hello,
    >
    >
    > With php4 I did the following in class initialisation :
    >
    > class base_class {
    >
    > function modify_polymorp hic($otherclass ){
    > $this=new $otherclass();
    > }
    >
    > function other_function( ){
    > echo "Base method";
    > }
    >
    > }
    >
    > class herited_class extends base_class {
    >
    > function other_function( ){
    > echo "Herited method";
    > }
    >
    > }
    >
    > $base=new base_class();
    > $base->modify_polymor phic("herited_c lass");
    > $base->other_function ();
    >
    > This will echo "Herited method" in PHP 4.
    > But in PHP 5 it does not wand to work at all.
    > PHP interpreter says :
    >
    > Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]

    That is correct. It's never been valid to reassign $this. But previous
    versions of PHP let it by.


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

    Comment

    • Mladen Gogala

      #3
      Re: Php 5 : polymorphic class

      On Mon, 10 Oct 2005 20:24:51 +0200, Baptiste Pillot wrote:
      [color=blue]
      > Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]

      $this is something that points to current object. Reassigning $this is
      like flying by pulling your hair. Ain't possible.

      --


      Comment

      • Tony Marston

        #4
        Re: Php 5 : polymorphic class


        "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
        news:SbudndpdiO jQJNfeRVn-iA@comcast.com. ..[color=blue]
        > Baptiste Pillot wrote:[color=green]
        >> Hello,
        >>
        >>
        >> With php4 I did the following in class initialisation :
        >>
        >> class base_class {
        >>
        >> function modify_polymorp hic($otherclass ){
        >> $this=new $otherclass();
        >> }
        >>
        >> function other_function( ){
        >> echo "Base method";
        >> }
        >>
        >> }
        >>
        >> class herited_class extends base_class {
        >>
        >> function other_function( ){
        >> echo "Herited method";
        >> }
        >>
        >> }
        >>
        >> $base=new base_class();
        >> $base->modify_polymor phic("herited_c lass");
        >> $base->other_function ();
        >>
        >> This will echo "Herited method" in PHP 4.
        >> But in PHP 5 it does not wand to work at all.
        >> PHP interpreter says :
        >>
        >> Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]
        >
        > That is correct. It's never been valid to reassign $this. But previous
        > versions of PHP let it by.[/color]

        You sample code also shows that you do not understand what "polymorphi sm"
        actually means.

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




        Comment

        • Jerry Stuckle

          #5
          Re: Php 5 : polymorphic class

          Tony Marston wrote:[color=blue]
          > "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
          > news:SbudndpdiO jQJNfeRVn-iA@comcast.com. ..
          >[color=green]
          >>Baptiste Pillot wrote:
          >>[color=darkred]
          >>>Hello,
          >>>
          >>>
          >>>With php4 I did the following in class initialisation :
          >>>
          >>>class base_class {
          >>>
          >>>function modify_polymorp hic($otherclass ){
          >>> $this=new $otherclass();
          >>>}
          >>>
          >>>function other_function( ){
          >>> echo "Base method";
          >>>}
          >>>
          >>>}
          >>>
          >>>class herited_class extends base_class {
          >>>
          >>>function other_function( ){
          >>> echo "Herited method";
          >>>}
          >>>
          >>>}
          >>>
          >>>$base=new base_class();
          >>>$base->modify_polymor phic("herited_c lass");
          >>>$base->other_function ();
          >>>
          >>>This will echo "Herited method" in PHP 4.
          >>>But in PHP 5 it does not wand to work at all.
          >>>PHP interpreter says :
          >>>
          >>>Fatal error: Cannot re-assign $this in scriptname.php on line 10[/color]
          >>
          >>That is correct. It's never been valid to reassign $this. But previous
          >>versions of PHP let it by.[/color]
          >
          >
          > You sample code also shows that you do not understand what "polymorphi sm"
          > actually means.
          >[/color]

          Noe my code...


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

          Comment

          Working...