dongna9185 2016-01-08 20:49
浏览 185
已采纳

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

I have one function that ends with:

return $authors;

How can I access this value in another function within the same class?

I've tried (in the second function)

$this->authors;

But it doesn't seem to do anything.

  • 写回答

1条回答 默认 最新

  • donglin6313 2016-01-08 21:02
    关注
    public function returnAuthor()
    {
       return $author;
    }
    
    public function anotherMethod()
    {
       return $this->returnAuthor();
    }
    

    You could follow a similar method as stated in the above example. or you could define a property for your class, have one function assign a value to it and have another function return the value

    Like so

    class Book
    {
       public $author;
    
       function __construct()
       {
          $this->author = 'Sir Conan Doyle';
       }
    
       function anotherMethod()
       {
          return $this->author;
       }
    }
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?