Re: Class Properties in Interfaces?

From: Date: Wed, 07 May 2008 13:22:13 +0000
Subject: Re: Class Properties in Interfaces?
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.internals 
Request: Send a blank email to internals+get-37511@lists.php.net to get a copy of this message
2008/5/6 Lars Strojny <lars@strojny.net>: > Hi Marcus! > > Am Dienstag, den 06.05.2008, 21:45 +0200 schrieb Marcus Boerger: > [...] > > > All fine with me. However we *would* need to specify which function is > > getter, setter, isset or unset. > > [...] > > > > > public $property { > > string public function __get() { > > return $this->_property; > > } > > string protected function __set(string $value) {...} > > } > > That's the variant I prefer. It is pretty similar to the C# does it and > therefore follows the common PHP strategy of steeling everything > together ;) > > [...] > > > The advantage of keeping everything inside the property definition is that > > there is no need at all for any new keyword. And the handlers cannot get > > separated. The disadvantage is that the functions are always named __get and > > so on for all properties, so PHP would need to do an internal renaming. > > Which means we probably would not be able to call the functions manually. > > Do you see any real use-case for calling them directly? > > > > That larger handlers could clutter the code doesn't appear to be a > > disadvantage for me as it can easily be avoided if the handler just > > forwards the call. > > Either that or folding in the editor helps etc. pp. > > > > The next question I have now is what to do when people want direct access > > to the underlying value? > > I would think that accessing $this->property from __get()/__set() of the > property would address the value itself. Everything else would be > redirected to the accessors. > > > > Or do we force people to always specify get,set,isset und unset? Or > > should we only enforce get/set and have isset > > and unset emulated with them (isset()~>isset(get()), > > unset()~>set(NULL))? > > Not sure about that. Forcing the user to define four accessors for a > property seems to be clutter but it would be - technically spoken - > correct. I don't have a fixed opinion here. > > cu, Lars > You would only normally need to define the four accessors if your property isn't a bog standard one or you didn't want to trigger any activity when a property changed. class foo { public $bar; } is fine. foo::bar is a completely normal property as we know it. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"

« previous php.internals (#37511) next »