我有一个以结尾的函数
return $authors;
如何在同一类中的另一个函数中访问此值?
我试过(在第二个功能中)
$this->authors;
但它似乎没有任何作用。
public function returnAuthor()
{
return $author;
}
public function anotherMethod()
{
return $this->returnAuthor();
}
您可以按照上面示例中所述的类似方法进行操作。或者你可以为你的类定义一个属性,让一个函数为它赋值,让另一个函数返回值
像这样
class Book
{
public $author;
function __construct()
{
$this->author = 'Sir Conan Doyle';
}
function anotherMethod()
{
return $this->author;
}
}
希望这能有所帮助!