Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > Attributes vs. Accessors
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 23rd November 18:20
philthathril
External User
 
Posts: 1
Default Attributes vs. Accessors



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
  Reply With Quote


 


2 23rd November 18:20
stuttle
External User
 
Posts: 1
Default Attributes vs. Accessors



If it's a public member variable there is no need for plain accessor
methods - they add no value. I feel the same about private variables
with plain get and set accessors, there's just no point unless the
accessors are doing more than setting and getting the internal variable.

-Stut

--
http://stut.net/
  Reply With Quote
3 23rd November 18:21
christoph.boget
External User
 
Posts: 1
Default Attributes vs. Accessors


Personally, I always use the accessor methods.

For the most part I agree with Stut -- they don't add much value.
Where it does add value, though, is in the eventuality you ever need
to change the attribute to a more restrictive visibility. If you ever
run across this need (and I have a couple of times), the only thing
affected is the class; you don't have to worry about changing all of
your code using that class. Conceptually, it is my preference to keep
a class as much of a black box as possible and accessor methods allow
for that.

thnx,
Christoph
  Reply With Quote
4 23rd November 18:21
stuttle
External User
 
Posts: 1
Default Attributes vs. Accessors


In my experience that's pretty rare, but your mileage may vary. If it
does happen you can simply implement __get and __set magic methods,
which is far better from a code maintainability point of view than
littering the class with pointless accessor methods just in case (IMHO).

-Stut

--
http://stut.net/
  Reply With Quote
5 23rd November 18:21
philthathril
External User
 
Posts: 1
Default Attributes vs. Accessors


1 - Attributes
1 - Accessors

Do continue... Hehehe =P

~Philip
  Reply With Quote
6 23rd November 18:21
eric.butera
External User
 
Posts: 1
Default Attributes vs. Accessors


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.
  Reply With Quote
7 23rd November 18:21
richard
External User
 
Posts: 1
Default Attributes vs. Accessors


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.


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
  Reply With Quote
8 23rd November 18:21
eric.butera
External User
 
Posts: 1
Default Attributes vs. Accessors


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)
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666