class problem

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

    class problem

    Hi,

    When executing the code below, expecting to say that the apple is green
    and later red I get the error

    Parse error: parse error, expecting `T_VARIABLE' in /httpdocs/test01.php
    on line 3

    Any idea how I can fix this?

    Thanks in advance,

    Roderik

    Code:
    <?php
    class apple {
    var color;
    
    function apple() {
    color = "green";
    }
    }
    
    class banana {
    var color;
    
    function banana() {
    color = "green";
    }
    }
    
    var red_apple = new apple();
    var yellow_banana = new banana();
    ?>
    
    <html>
    <head></head>
    <body>
    <?
    print("The color of our apple is ".red_apple->color);
    red_apple->color = "red";
    print("Some weeks later the color of our apple is ".red_apple->color);
    ?>
    
    </body>
    </html>
    --
    Web development en webhosting

    webdesign, internet applicaties, internetgestuur de elektronica
  • bruno modulix

    #2
    Re: class problem

    Roderik a écrit :[color=blue]
    > Hi,
    >
    > When executing the code below, expecting to say that the apple is green
    > and later red I get the error
    >
    > Parse error: parse error, expecting `T_VARIABLE' in /httpdocs/test01.php
    > on line 3
    >
    > Any idea how I can fix this?
    >
    >
    > [code]
    > <?php
    > class apple {
    > var color;[/color]
    var $color;[color=blue]
    >
    > function apple() {
    > color = "green";[/color]
    $color = "green";

    but doing so, $color will be a local variable.
    you want :
    $this->color = "green";

    And take some time to read the manual -- it may help.

    Bruno

    Comment

    • Roderik

      #3
      Re: class problem

      bruno modulix wrote:
      [color=blue]
      > Roderik a écrit :
      >[color=green]
      >> Hi,
      >>
      >> When executing the code below, expecting to say that the apple is
      >> green and later red I get the error
      >>
      >> Parse error: parse error, expecting `T_VARIABLE' in
      >> /httpdocs/test01.php on line 3
      >>
      >> Any idea how I can fix this?
      >>
      >>
      >> [code]
      >> <?php
      >> class apple {
      >> var color;[/color]
      >
      > var $color;
      >[color=green]
      >> function apple() {
      >> color = "green";[/color]
      >
      > $color = "green";
      >
      > but doing so, $color will be a local variable.
      > you want :
      > $this->color = "green";
      >
      > And take some time to read the manual -- it may help.
      >
      > Bruno[/color]

      excuse me, I made very dirty code for the example now. I'll post a new
      one. The problem is still there.


      --
      http://www.archytas.nl/
      webdesign, internet applicaties, internetgestuur de elektronica

      Comment

      • Roderik

        #4
        Re: class problem

        Hi,

        Here again the question with some more clear (but still errornous) code.
        I get the error:

        Parse error: parse error in /httpdocs/test01.php on line 10

        Any idea what the problem might be?

        Thanks in advance,

        Roderik

        Code:
        <?php
        class apple {
        var $color;
        
        function apple() {
        $this->color = "green";
        }
        }
        
        var red_apple = new apple();  // line 10
        //var yellow_banana = new banana();
        ?>
        
        <html>
        <head></head>
        <body>
        <?
        print("The color of our apple is ".$red_apple->color);
        red_apple->color = "red";
        print("Some weeks later the color of our apple is ".$red_apple->color);
        ?>
        </body>
        </html>

        Comment

        • Roderik

          #5
          Re: class problem

          Roderik wrote:
          [color=blue]
          > Hi,
          >
          > Here again the question with some more clear (but still errornous) code.
          > I get the error:
          >
          > Parse error: parse error in /httpdocs/test01.php on line 10
          >
          > Any idea what the problem might be?
          >
          > Thanks in advance,
          >
          > Roderik
          >
          >
          Code:
          > <?php
          > class apple {
          >     var $color;
          >
          >     function apple() {
          >         $this->color = "green";
          >     }
          > }
          >
          > var red_apple = new apple();  // line 10
          > //var yellow_banana = new banana();
          > ?>
          >
          > <html>
          > <head></head>
          > <body>
          >         <?
          >  print("The color of our apple is ".$red_apple->color);
          >  red_apple->color = "red";
          >  print("Some weeks later the color of our apple is ".$red_apple->color);
          > ?>
          >     </body>
          > </html>
          >
          >[/color]

          Ow now I see, I am not commercially enough. I still forgot to place some
          dollar signs. But it is fixed now.

          --
          Web development en webhosting

          webdesign, internet applicaties, internetgestuur de elektronica

          Comment

          • Zurab Davitiani

            #6
            Re: class problem

            Roderik wrote:
            [color=blue]
            > Parse error: parse error in /httpdocs/test01.php on line 10[/color]
            <snip>[color=blue]
            > var red_apple = new apple(); // line 10[/color]


            Comment

            • bruno modulix

              #7
              Re: class problem

              Roderik a écrit :[color=blue]
              > Hi,
              >
              > Here again the question with some more clear (but still errornous) code.
              > I get the error:
              >
              > Parse error: parse error in /httpdocs/test01.php on line 10
              >
              > Any idea what the problem might be?
              >
              > Thanks in advance,
              >
              > Roderik
              >
              > [code]
              > <?php
              > class apple {
              > var $color;
              >
              > function apple() {
              > $this->color = "green";
              > }
              > }
              >
              > var red_apple = new apple(); // line 10[/color]

              s/var//
              s/red_apple/$red_apple/

              And definitively : RTFM !

              Bruno

              Comment

              Working...