Php 如何在另一个函数中访问一个函数的返回值?

Php 如何在另一个函数中访问一个函数的返回值?,php,Php,我有一个函数的结尾是: return $authors; 如何在同一类中的另一个函数中访问此值 我尝试过(在第二个函数中) 但它似乎没有任何作用 public function returnAuthor() { return $author; } public function anotherMethod() { return $this->returnAuthor(); } 您可以遵循上面示例中所述的类似方法。 或者,您可以为类定义一个属性,让一个函数为其赋值,然后让另

我有一个函数的结尾是:

return $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;
   }
}

希望这有帮助

要么将其作为参数传递给下一个方法,要么首先将
$autor
分配给类属性,例如
$this->author=$author你。。。。调用函数?假设您的函数名为
getAuthor
。。然后您将调用
$this->getAuthor()
@ccKep,对吧,但是如果我们假设的getAuthor()需要运行参数,我们如何将getAuthor()中设置的现有参数传递给第二个函数,以便再次运行getAuthor()。如果我想将其作为一个可以使用的变量返回,该怎么办。我在第二个函数中尝试了这一点,但没有得到任何结果:
$authorList=$this->authors;变量转储($authorList)$authors是一个数组,如果这很重要的话。老实说,就是这样,如果你得到null,它一定是其他东西。看起来你没有做错什么。没有你的代码很难说。看看->
class Book
{
   public $author;

   function __construct()
   {
      $this->author = 'Sir Conan Doyle';
   }

   function anotherMethod()
   {
      return $this->author;
   }
}