On Fri, 2010-04-23 at 11:57 -0400, Adam Richardson wrote: > On Fri, Apr 23, 2010 at 6:19 AM, Richard Quadling > <rquadling@xxxxxxxxxxxxxx>wrote: > > > On 23 April 2010 10:55, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx> wrote: > > > I know the subject line might sound a bit like an oxymoron, but bear > > > with me! > > > > > > What I'd like to have is a property of an object that behaves like a > > > public variable when it comes to reading, but like a protected one when > > > it comes to writing, as it would make my code a lot easier to read. > > > > > > I know about the __get($var) method to magically provide this behaviour, > > > but from all I've read it can be pretty slow compared to a regular > > > public variable. I've seen a thread on the dev lists where someone > > > requested it, but can't find anything in the manual. > > > > > > Does anyone know if this is available in a later version of PHP, or if > > > it's implementation is penned for some time in the future? > > > > > > Thanks, > > > Ash > > > http://www.ashleysheridan.co.uk > > > > > > > > > > > > > What you are asking for is commonly known as accessibility. This has > > been discussed on the list and is something I think would be a great > > feature. > > > > http://wiki.php.net/rfc/propertygetsetsyntax is an incomplete RFC (it > > says it is incomplete at the top). > > > > The last update was 2010/01/08 22:11, so a few months old. > > > > Hopefully, some of the clever brains here can get to grips with it and > > come to some consensus. > > > > > > Whilst you can easily code __get()/__set() or getVar()/setVar() > > methods to deal with it, with the later ones being significantly > > easier to docblock, there is no easy way to document the property, > > only the methods to set or get the property. > > > > > > Richard. > > > > > > > > > > > > > > -- > > ----- > > Richard Quadling > > "Standing on the shoulders of some very clever giants!" > > EE : http://www.experts-exchange.com/M_248814.html > > EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp > > Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 > > ZOPA : http://uk.zopa.com/member/RQuadling > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > I've sometimes implemented a scheme where I create the variable, then unset > it in the constructor and rely on the magic method. I know you were > concerned about the performance of magic methods, and I don't have a > solution for that :( > > For example: > > <?php > /** > * Description of PHPClass that documents nicely and still protects > variables from reassignment. > * > * @author Adam Richardson > */ > class PHPClass { > /** > * Name for object. > * @var String > */ > public $name; > /** > * Size of something really important. > * @var int > */ > public $size; > /** > * Local storage of magic vars > * @var array > */ > private $_vars = array(); > /** > * Create object and initialize instance vars. > * @param array $instance_vars > */ > function __construct(array $instance_vars) > { > unset($this->name); > unset($this->size); > > $this->_vars = $instance_vars; > } > function __get($name) > { > return $this->_vars[$name]; > } > } > > $obj = new PHPClass($instance_vars = array('name' => 'Billy', 'size' => > 1000)); > > echo $obj->name; > > ?> > > Adam > I think for now I'll just resort to leaving it as a public variable. I'll leave the specific set function for it in and just hope that is used instead! As it's only me who'll be using it for the time being, I can always yell at myself later if I forget! Thanks, Ash http://www.ashleysheridan.co.uk