PHP "parse the error, expecting` T_STRING '"- Please help with the code

I am trying to create a dynamic php site using MVC principles. I am using WAMP for Win XP.

Here is 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. in the declaration of the get function. Error message:

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

Help would be much appreciated ....

Thank,

Jdelage

+3
source share
5 answers
function $getUserName()

it should be

function getUserName()

After the keyword, functionPHP expects a space followed by identifier(function name). But in your case it finds variable, and this leads to a parsing error .

T_STRING, T_VARIABLE. PHP :

: , T_VARIABLE, T_STRING....

, , :

: , `T_STRING '....

+4

. $getUserName, getUserName ( $).

+3

function $hit> getUserName()

+3
source

You cannot use variable substitution when declaring functions.

It:

function $getUserName()

should be as follows:

function getUserName()
+3
source

Function names at the beginning do not require the $ sign.

Do it like this:

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

Source: https://habr.com/ru/post/1763299/


All Articles