Re: Attributes vs. Accessors

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

 



On Mon, Aug 25, 2008 at 11:01 AM, Richard Heyes <richard@xxxxxxx> wrote:
>>> Curious. Which do you prefer and why?
>
> Accessor methods. They allow for changes in the future that may well
> be unforeseen at the moment. Or at least that would be my response
> with PHP4. Now with the __get and __set built-in accessors, that's
> pretty much taken care of.
>
>> 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,
>
> Not tried this, but you may be able to do it with a __get method that
> doesn't return anything.
>
> --
> Richard Heyes
> http://www.phpguru.org
>

Oh it'd be possible, but all this does is distract me from the purpose
of my code.  I am easily distracted though seeing as I'm writing this
out in the first place!  :)  A simple note in the docs saying this is
read only is enough for me.  You'd have to put a note in there about
how it will throw an exception upon write anyways.  *shrug*  But again
these are very specific pieces of code that are getting public
properties.

This goes in line with all the other coding practices I try to use.
Registry over singleton, notification queue over direct observer, etc.
 Let something else do the heavy lifting when possible.


class readonlytest {

	private $readonly = array('meh');
	private $meh = 'meh value';
	public $blah;
	
	private function __get($name) {
		if (in_array($name, $this->readonly)) {
			return $this->{$name};
		}
	}
	
	private function __set($name, $value) {
		if (in_array($name, $this->readonly)) {
			throw new Exception("The property {$name} is read only");
		}
		$this->{$name} = $value;
	}

}

$test = new readonlytest;

echo "Pre-blah:";
var_dump($test->blah);
$test->blah = 'blah';
echo "Post-blah:";
var_dump($test->blah);

echo "Reading meh:";
var_dump($test->meh);
try {
	$test->meh = 'meh';
} catch (Exception $e) {
	echo "Exception:";
	var_dump($e);
}
var_dump($test->meh);

But why would I want to clutter up my class with that nonsense?

Output:

Pre-blah:
null
Post-blah:
string 'blah' (length=4)
Reading meh:
string 'meh value' (length=9)
Exception:
object(Exception)[2]
  protected 'message' => string 'The property meh is only' (length=24)
  private 'string' => string '' (length=0)
  protected 'code' => int 0
  protected 'file' => string '/Users/eric/Sites/blah.php' (length=26)
  protected 'line' => int 18
  private 'trace' =>
    array
      0 =>
        array
          'file' => string '/Users/eric/Sites/blah.php' (length=26)
          'line' => int 36
          'function' => string '__set' (length=5)
          'class' => string 'readonlytest' (length=12)
          'type' => string '->' (length=2)
          'args' =>
            array
              0 => string 'meh' (length=3)
              1 => string 'meh' (length=3)
string 'meh value' (length=9)

-- 
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