Jump to content

[SOLVED] What's wrong with this code?


mindspin311

Recommended Posts

I get: Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /root/test.php on line 13

 

I just want to make a person object with several variables similar to a struct in c++. First, what's wrong with the code, and 2nd is their an easier way to do this without the set and get functions?

 

I want to do this...

 

struct Person
{
  string id;
  string name;
  string score;
  .....
};

Person bob;
bob.id = "1234";
bob.name = "bob";
bob.score = 34;

printf( "%s : %s   %d",bob.id,bob.name,bob.score);

<?php



class person
{
  var $m_id = NULL;
  var $m_name = NULL;
  var $m_score = NULL;

  function id()
  {
    return this->m_id;
  }
  function name()
  {
    return this->m_name;
  }
  function score()
  {
    return this->m_score;
  }
  function set_id($id)
  {
    this->m_id = $id;
  }
  function set_name($name)
  {
    this->m_name = $name;
  }
  function set_score($score)
  {
    this->m_score = $score;
  }

}

$bob = new person();
$bob->set_id('123456789');
$bob->set_name('bob');
$bob->set_score(88);


echo $bob->id()." (".$bob->name()."): ".$bob->score()." <br>";

?>

Link to comment
https://forums.phpfreaks.com/topic/89874-solved-whats-wrong-with-this-code/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.