php function in function -


i trying build function call function. example, if have array full of function names call, possible call function every array value without writing in script?

example:

function email($val=null) {     if($val)          $this->_email = $val;     else          return $this->_email; }  function fname($val=null) {     if($val)          $this->_fname = $val;     else          return $this->_fname; }  

for email fname etc.

but want have like:

function contr_val($key,$val) {      function $key($val=null) {         if($val)              $this->_$key = $val;         else              return $this->_$key;     }     function $key($val="hallo"); } 

and call with:

contr_val("email","test") 

what you're trying create member variables dynamically , retrieve values. __get() , __set() for*.

here's how use it:

class testclass {   var $data = array();    public function __set($n, $v) { $this->data[$n] = $v; }   public function __get($n) {     return (isset($this->data[$n]) ? $this->data[$n] : null);   }    public function contr_val($k,$v = null) {     if ($v) $this->$k = $v;     else return $this->$k;   } };  $sherp = new testclass; $sherp->contr_val("herp", "derp"); echo "herp is: " . $sherp->contr_val("herp") . "\n"; echo "narp is: " . $sherp->contr_val("narp") . "\n"; 

**afuzzyllama beat me point, felt code example necessary.*


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -