Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to build a dynamic php site using MVC principles. I'm using WAMP on Win XP.

Here's the code:

Index.php:

<?php
  require_once("User.php");
  session_start();
  include ("Header.php");
  include ("Footer.php");
?>

Header.php:

<?php
  echo '<?xml version="1.0" encoding="utf-8"?>'
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
  <title><?php echo "temp"; ?></title>
  <link rel="stylesheet" media="screen" type="text/css" title="StyleSheetProjet" href="StyleSheetProjet.css" />
</head>
<body>
  <div class="connectionStatus">
    <?php
    ?>
  </div>

Footer.php:

</body>
</html>

User.php:

<?php
  class User {
    private $userName;
    private $hashedPassword;
    private $firstName;
    private $lastName;
    private $userEmail;

    function $getUserName() {
      return $this->userName;
    }
  }
?>

This code causes a php error on line 9 of User.php, i.e., at the get function declaration. The error message is:

Parse error: parse error, expecting `T_STRING' in C:\wamp\www\Projet\User.php on line 9

Help would be very much appreciated....

Thanks,

JDelage

share|improve this question

5 Answers

up vote 3 down vote accepted
function $getUserName()

should be

function getUserName()

After the function keyword, PHP expects a whitespace followed by an identifier(function name). But in your case it finds a variable and this causes a parse error.

Internally an identifier is called T_STRING and a variable is called T_VARIABLE. Some PHP interpreters throw:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING ....

Others, as in your case just say:

Parse error: parse error, expecting `T_STRING' ....

share|improve this answer

You have typo in your function name. Currently it's $getUserName where it should be getUserName (without $).

share|improve this answer

You can't use variable substitution when declaring functions.

This:

function $getUserName()

should be this:

function getUserName()
share|improve this answer

Function names does not need $ sign at the beginning.

Do it like that:

function getUserName() {
  return $this->userName;
}
share|improve this answer
How freaking stupid of me... Thanks for that. :-) – JDelage Sep 5 '10 at 7:06

function$getUserName()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.