Dumb experiment - creating C#-esque properties in PHP -


i'm trying come graceful way create c#-esque properties in php

right now, have:

class example {     private $allowedprops = array('prop1', 'prop2', 'prop3');     private $data = array();      public function __set($propname, $propvalue)     {         if (in_array($propname, $this->allowedprops))         {             // set property         }         else         {             // error         }     }      public function __get($propname)     {         if (array_key_exists($propname, $this->data))         {             // property         }         else         {             // error         }     } } 

in commented out sections, more complex writing or retrieving $data array, easiest thing branch logic necessary if-else or switch. that's messy, though, , runs counter want. there way invoke callback function have access $data array?


edit: seems simplest thing be:

class example {     private $allowedprops = array('prop1', 'prop2', 'prop3');     private $data = array();      public function __set($propname, $propvalue)     {         $propname = strtolower($propname);          if (in_array($propname, $this->allowedprops))         {             $funcname = "set" . ucfirst($propname);             $this->$funcname($propvalue);         }         else         {             // error         }     }      public function __get($propname)     {         $propname = strtolower($propname);          if (array_key_exists($propname, $this->data))         {             $funcname = "get" . ucfirst($propname);             $this->$funcname();         }         else         {             // error         }     }      private function getprop1()     {         // stuff, , return value of prop1, if exists     }      // ... } 

i'm not sure if having host of private setter , getter methods way go. lambdas ideal, they're available in php 5.3+.

not answer, per se, figured i'd mark solved haven't had time experiment further. of course, additional suggestions welcome.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -