Re: [PHP] Two ways to obtain an object property

2012-08-16 Thread Mihamina Rakotomandimby

On 08/15/2012 11:28 AM, phplist wrote:

This relates to a minor dilemma I come across from time and time, and
I'm looking for advice [...]

Within a site I have a User object, and within page code would like to have
if ($crntUser-isASubscriber) {...}
[...]
if ($crntUser-isASubscriber()) {...}
[...]
Is either of these approaches preferable, or does it simply not matter?



It depends
- how long/heavy is isASubscriber()
- how often you need to check
- how realtime you need the subscription status to be

if isASubscriber() is long/heavy (say you need to wait for a long 
query), I would suggest to set an attribute and get you information from 
the attribute.

Otherwise, you can safely use the method.

If you have realtime constraints, you have no choice than use the 
method, wether it's long or not.


Talking about best practice, I would use:
- $currentUser-isSubscriber
- $currentUser-isSubscriber()
It's a naming matter.



--
RMA.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Two ways to obtain an object property

2012-08-16 Thread phplist

On 08/15/2012 11:28 AM, phplist wrote:

This relates to a minor dilemma I come across from time and time, and
I'm looking for advice [...]

Within a site I have a User object, and within page code would like to
have
if ($crntUser-isASubscriber) {...}
[...]
if ($crntUser-isASubscriber()) {...}
[...]
Is either of these approaches preferable, or does it simply not matter?



Thanks to all who responded. Inevitably the answer is it depends but 
I've now got a much better feel about how to decide.


Roddie Grant

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Two ways to obtain an object property

2012-08-15 Thread phplist
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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Two ways to obtain an object property

2012-08-15 Thread Sebastian Krebs
Hi,

2012/8/15 phplist phpl...@myword.co.uk

 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




Re: [PHP] Two ways to obtain an object property

2012-08-15 Thread Paul M Foster
On Wed, Aug 15, 2012 at 09:28:28AM +0100, phplist wrote:

 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?

If I read you correctly, the latter would be preferable, primarily
because it involves one less step. But if you're going to have to make
that decision in an if statement repeatedly, I'd probably say:

$isSubscribed = $crntUser-isASubscriber();

just because in subsequent code, you're not suffering the (admittedly
small) repeated overhead of the function call.

But in answer to your question, isASubscriber() would be a pretty
standard getter method to expose internal object properties.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Two ways to obtain an object property

2012-08-15 Thread Andrew Ballard
On Wed, Aug 15, 2012 at 11:33 AM, Paul M Foster pa...@quillandmouse.com wrote:
 On Wed, Aug 15, 2012 at 09:28:28AM +0100, phplist wrote:

 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?

 If I read you correctly, the latter would be preferable, primarily
 because it involves one less step. But if you're going to have to make
 that decision in an if statement repeatedly, I'd probably say:

 $isSubscribed = $crntUser-isASubscriber();

 just because in subsequent code, you're not suffering the (admittedly
 small) repeated overhead of the function call.

 But in answer to your question, isASubscriber() would be a pretty
 standard getter method to expose internal object properties.

 Paul

I generally use this getter method as well. If it bothers the OP
that isASubscriber() is a method rather than a property, one could
use the magic __get method:

?php

class User
{
private $isASubscriber = false;

public function __get($property)
{
if ('isASubscriber' === $property) {
return $this-isASubscriber;
}
}
}

?

In the end, it is still a function call either way though. The
advantage I see to both of these method-based approaches versus
exposing the property as public is that these methods prevent code
outside your class from doing something stupid:

?php

$u = new User();

$u-isASubscriber = 'rutabaga';

// Fatal error: Cannot access private property User::$isASubscriber
?

(Yes, I have seen proof that it is TECHNICALLY possible to get around
this in PHP, for example with unserialize.)

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Two ways to obtain an object property

2012-08-15 Thread David Harkness
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.

Peace,
David