Re: Attributes vs. Accessors
| From: | Eric Butera | Date: | Mon, 25 Aug 2008 13:13:53 +0000 |
| Subject: | Re: Attributes vs. Accessors | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-278660@lists.php.net to get a copy of this message | ||
On Mon, Aug 25, 2008 at 8:32 AM, Philip Thompson <philthathril@gmail.com> wrote:
> Hi all.
>
> Curious. Which do you prefer and why?
>
> <?php
> class Hello {
> public $hi;
>
> function __construct () {
> $this->hi = 'Well Hello There!';
> }
>
> function hi () {
> return $this->hi;
> }
> }
>
> $hello = new Hello ();
>
> // Access the value this way...
> echo $hello->hi;
>
> // or the accessor...
> echo $hello->hi();
> ?>
>
> For publicly-declared variables, do you access the attribute directly or use
> an accessor?
>
> ~Philip
I access directly to avoid pointless method calls for reads. It'd be
nice if there were a way to define a public read-only mode, but in my
code it is more of a rule of thumb. I'm pretty strict on what gets
public visibility though.