Re: Two ways to obtain an object property

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

2012/8/15 phplist <phplist@xxxxxxxxxxxx>

> 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()) {...}
>

or you just set User::$isASubscriber before.
or you can utilise __get()

I don't really get the problem. You can for example set the property right
after instanciation, or during instanciation (constructor).

$this->isASubscriber = !empty($subscriptions); // or something like that

Regards,
Sebastian


>
> 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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux