PHP“解析错误,期待'T_STRING'” - 请帮助代码

时间:2010-09-05 06:59:46

标签: php wamp

我正在尝试使用MVC原则构建一个动态的php站点。我在Win XP上使用WAMP。

以下是代码:

的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;
    }
  }
?>

此代码在User.php的第9行导致php错误,即在get函数声明处。错误消息是:

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

非常感谢帮助......

谢谢,

JDelage

5 个答案:

答案 0 :(得分:4)

function $getUserName()

应该是

function getUserName()

function关键字之后,PHP需要一个空格后跟一个identifier(函数名)。但在您的情况下,它会找到variable,这会导致解析错误

在内部,标识符称为T_STRING,变量称为T_VARIABLE。一些PHP解释器抛出:

  

解析错误:语法错误,意外T_VARIABLE,期待T_STRING ....

其他人,就像你的情况一样:

  

解析错误:解析错误,期待`T_STRING'....

答案 1 :(得分:3)

您的函数名称中有拼写错误。目前它是$getUserName,它应该是getUserName(没有$)。

答案 2 :(得分:3)

function <击> $ getUserName()

答案 3 :(得分:3)

在声明函数时,不能使用变量替换。

此:

function $getUserName()

应该是这样的:

function getUserName()

答案 4 :(得分:2)

函数名称在开头不需要$ sign。

这样做:

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