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

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

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
    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!

    点赞 评论

相关推荐