Php 为什么字符串插值会抛出未定义的属性

Php 为什么字符串插值会抛出未定义的属性,php,string-interpolation,Php,String Interpolation,我有这个代码,我有一个对象狗,它扩展了动物 当我在Dog类中使用字符串插值来访问Animal类中的方法时,我遇到了问题,但当我只是连接时,一切都正常。为什么 示例代码: <?php class Animal { private $name; public function getName():string { return $this->name; } public function setName($value) { $this->n

我有这个代码,我有一个对象狗,它扩展了动物

当我在Dog类中使用字符串插值来访问Animal类中的方法时,我遇到了问题,但当我只是连接时,一切都正常。为什么

示例代码:

<?php

class Animal
{
  private $name;
  public function getName():string
  {
    return $this->name;
  }
  public function setName($value)
  {
    $this->name=$value;
  }
}

class Dog extends Animal
{
  public function Walk()
  {
    echo $this->getName() ." is walking."; //This line works
    echo "$this->getName() is walking."; //This line throws the exception Notice: Undefined property: Dog::$getName in C:\xampp\htdocs\stackoverflow\question1\sample.php on line 27 () is walking.
  }
}

$dog = new Dog();
$dog->setName("Snoopy");
$dog->Walk();

 ?>

将函数调用用括号括起来:

class Dog extends Animal
{
  public function Walk()
  {
    echo $this->getName() ." is walking."; 
    echo "{$this->getName()} is walking."; 
  }
}

将函数调用用括号括起来:

class Dog extends Animal
{
  public function Walk()
  {
    echo $this->getName() ." is walking."; 
    echo "{$this->getName()} is walking."; 
  }
}

因为
$this->getName
是可以作为变量处理的符号的完整列表。普通插值仅适用于少数受约束的变量访问,而不适用于任意表达式。请参阅,因为
$this->getName
是可以作为变量处理的符号的完整列表。普通插值仅适用于少数受约束的变量访问,而不适用于任意表达式。看见