为什么字符串插值会抛出 Undefined 属性
Why string interpolation throws Undefined property
我有这段代码,其中有一个扩展 Animal 的对象 Dog。
当我在 Dog class 中使用字符串插值来访问 Animal class 中的方法时,我遇到了问题,但是当我只是连接时,一切正常。为什么²
示例代码:
<?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\Whosebug\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.";
}
}
我有这段代码,其中有一个扩展 Animal 的对象 Dog。
当我在 Dog class 中使用字符串插值来访问 Animal class 中的方法时,我遇到了问题,但是当我只是连接时,一切正常。为什么²
示例代码:
<?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\Whosebug\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.";
}
}