Two ways to obtain an object property
| From: | phplist | Date: | Wed, 15 Aug 2012 08:28:28 +0000 |
| Subject: | Two ways to obtain an object property | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-318703@lists.php.net to get a copy of this message | ||
This relates to a minor dilemma I come across from time and time, and I'm looking for advice on pros and cons and best practice. Last night I encountered it again.
Within a site I have a User object, and within page code would like to have
if ($crntUser->isASubscriber) {...}
There seems to be two ways to handle this:
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()) {...}
While this is last night's specific example, I seem to face the method-setting-variable or the method-returning-result-directly decision quite often.
Is either of these approaches preferable, or does it simply not matter?
Thanks
Roddie Grant