Object Oriented Design: Return Values ​​or Set Properties?

What will be considered "best practice" in this case. I have a class that collects remote resources, and it looks something like this:

class Gather {
    public function getAll($locations) {
        $results = array('All','My','Results');
        return $results;
    }
}

My question is, should I consider it best to return the results or assign them as a property? i.e.

// This
$results = $gatherer->getAll();
// vs This
$gatherer->getAll(); // now $gatherer->results can be used

Most likely, I'm just overdoing it, but I don't have formal training, and I am wondering if there is a “more correct” way to do something like this.

+3
source share
2 answers

, ($ results = $gatherer- > getAll()) . , , , . , $gatherer- > results getAll(). , - , , .

. getResults() , , getResults(), , .

+6

.

$gatherer->getAll(); // now $gatherer->results can be used

$gatherer->initResults();

, $gatherer. $gatherer- > initResults() :

public function initResults() {
    $this->results = $this->getAll();
}

public function getAll() {
    // do whatever to get results
}

, .

, , , , , .

+1

Source: https://habr.com/ru/post/1789798/


All Articles