By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

class problem

P: n/a
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class apple {
  3. var color;
  4.  
  5. function apple() {
  6. color = "green";
  7. }
  8. }
  9.  
  10. class banana {
  11. var color;
  12.  
  13. function banana() {
  14. color = "green";
  15. }
  16. }
  17.  
  18. var red_apple = new apple();
  19. var yellow_banana = new banana();
  20. ?>
  21.  
  22. <html>
  23. <head></head>
  24. <body>
  25. <?
  26. print("The color of our apple is ".red_apple->color);
  27. red_apple->color = "red";
  28. print("Some weeks later the color of our apple is ".red_apple->color);
  29. ?>
  30.  
  31. </body>
  32. </html>
  33.  
--
http://www.archytas.nl/
webdesign, internet applicaties, internetgestuurde elektronica
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Roderik a écrit :
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; var $color;
function apple() {
color = "green";

$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
Jul 17 '05 #2

P: n/a
bruno modulix wrote:
Roderik a écrit :
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;


var $color;
function apple() {
color = "green";


$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


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, internetgestuurde elektronica
Jul 17 '05 #3

P: n/a
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class apple {
  3. var $color;
  4.  
  5. function apple() {
  6. $this->color = "green";
  7. }
  8. }
  9.  
  10. var red_apple = new apple();  // line 10
  11. //var yellow_banana = new banana();
  12. ?>
  13.  
  14. <html>
  15. <head></head>
  16. <body>
  17. <?
  18. print("The color of our apple is ".$red_apple->color);
  19. red_apple->color = "red";
  20. print("Some weeks later the color of our apple is ".$red_apple->color);
  21. ?>
  22. </body>
  23. </html>
  24.  
Jul 17 '05 #4

P: n/a
Roderik wrote:
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

Expand|Select|Wrap|Line Numbers
  1.  <?php
  2.  class apple {
  3.      var $color;
  4.      function apple() {
  5.          $this->color = "green";
  6.      }
  7.  }
  8.  var red_apple = new apple();  // line 10
  9.  //var yellow_banana = new banana();
  10.  ?>
  11.  <html>
  12.  <head></head>
  13.  <body>
  14.          <?
  15.   print("The color of our apple is ".$red_apple->color);
  16.   red_apple->color = "red";
  17.   print("Some weeks later the color of our apple is ".$red_apple->color);
  18.  ?>
  19.      </body>
  20.  </html>
  21.  


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

--
http://www.archytas.nl/
webdesign, internet applicaties, internetgestuurde elektronica
Jul 17 '05 #5

P: n/a
Roderik wrote:
Parse error: parse error in /httpdocs/test01.php on line 10 <snip> var red_apple = new apple(); // line 10


http://www.php.net/manual/en/language.variables.php
Jul 17 '05 #6

P: n/a
Roderik a écrit :
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


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

And definitively : RTFM !

Bruno
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.