On Wed, Aug 15, 2012 at 1:28 AM, phplist <phpl...@myword.co.uk> wrote:

> I can have a User object method "getSubscriberStatus()" which sets
> $this->isASubscriber. But to use this I would have to run the method just
> before the if statement.
> Or I could have a method "isASubscriber()" which returns the result,
> meaning the if statement should be
> if ($crntUser->isASubscriber()) {...}

I assume that the decision isn't really about using a property versus a
method but that determining the value of the property is costly. For these
cases I add an internal private property to save the result of the costly
computation and initialize it on the first call to the accessor method.

    class User {
        private $_isSubscriber = null;

        public function isSubscriber() {
            if ($this->_isSubscriber === null) {
                $this->_isSubscriber = ... call database or whatever takes
so long ...
            return $this->_isSubscriber;

If this isn't the case and you really just want to know which API is nicer,
any of the replies so far are acceptable. I favor accessor methods because
it's easier to change the implementation without having to change all the
places you access it.


Reply via email to