Search code examples
phpsyntaxtostring

PHP parse error: syntax error, unexpected '"."' (T_CONSTANT_ENCAPSED_STRING), expecting ';'


class Animal{
public $name;
public $color;
public function __construct($name,$color){

      $this -> name=$name;
      $this -> color=$color;
      }
      public function intro(){
      echo "<br> The animal is {$this -> name} and the color is {$this -> color}.";
      }
        public function __toString() { //89th line
            return "Name of the object is " .$this ->name ".";
      }

}

I cant run my code because of the "PHP Parse error: syntax error, unexpected '"."' (T_CONSTANT_ENCAPSED_STRING), expecting ';' in /home/yKJgGf/prog.php on line 89". But i think this code is not false. I cant find the solution how to fix this error.


Solution

  • <?php
    class Animal{
    public $name;
    public $color;
    public function \__construct($name,$color){
    
          $this -> name=$name;
          $this -> color=$color;
          }
          public function intro(){
          echo "<br> The animal is {$this -> name} and the color is {$this -> color}.";
          }
            public function __toString() { //89th line
                return "Name of the object is " . $this ->name . ".";
          }
    
    }
    

    Missing a dot after $this->name to append the remaining string.