[nycphp-talk] __get __set methods..
Alex C
alexchan.1976 at gmail.com
Wed Apr 20 11:49:30 EDT 2005
I really think these methods might be really handy for predefined
properties. Am I wrong to think that? just want to understand why this
might be bad idea to have these functions work with predefined
properties. with this runner class, i want to exclusively have
predefine properties for this object.
thanks everybody
alex
On 4/20/05, Christopher Hendry <chendry at gmail.com> wrote:
> I'm still kinda wondering tho - as nifty as all this is - on the
> engine level how much overhead is involved here. In the example
> below, if instantiating with an array of data, we have an 'is_array' a
> 'foreach' then a call to magic '__set', which then does an 'isset' ...
>
> Hmm...certainly there are times when you want your objects to act on
> unknown data, but for the most part, and certainly this Runner example
> - shouldn't we bind the data to the object and thus predefine the
> properties?
>
> On 4/19/05, Daniel Krook <krook at us.ibm.com> wrote:
> > Actually, the key to the whole thing (also from Adam's book, pp 261-263)
> > is to define what values are acceptable as properties in the constructor.
> > In this example only valOne and valTwo can be set and get:
> >
> > class Runner {
> >
> > private $data;
> >
> > public function __construct($runner = null) {
> > $this->data = array(
> > 'valOne' => 0,
> > 'valTwo' => ''
> > );
> > if (is_array($runner)) {
> > foreach ($runner as $field => $value) {
> > $this->$field = $value;
> > }
> > }
> > }
> >
> > public function __set($property, $value) {
> > if (isset($this->data[$property])) {
> > $this->data[$property] = $value;
> > }
> > }
> >
> > public function __get($property) {
> > if (isset($this->data[$property])) {
> > return $this->data[$property];
> > } else {
> > return false;
> > }
> > }
> > }
> >
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>
More information about the talk
mailing list