Automatically use getters and setters with PHP's __get and __set methods?
I am trying to build a base class that all of my classes extend from that allows for property access through simple $obj->property syntax, but if get_property() or set_property($value) is defined, then any attempt to get or set that property is routed through those getters and setters. 
The tricky part, though, is that I would like updates to object properties to be reflected in an array (which is a property of the object, call it $changed_array) which can output an array of the properties that were changed, for some purpose, say, insertion into a db update call.
The problem lies in this sample:
class Sample {
     private $changed_array;
     public __get($var_ame){
         if(method_exists($this, $method = 'get_' . $var_name)){
             return $this->$method();
         } else {
             return $this->$var_name;
         }
     }
     public __set($var_name, $value){
         if(method_exists($this, $method = 'set_' . $var_name))}
             return $this->$method($value);
         } else {
             // pseudo code
             if($this->$var_name isset and isn't $value) { // add to $changed_array }
             return $this->$var_name = $value;
         }
     }
}
Which works great, until there is a setter method defined like so:
public set_var_name($value){
    // pretend we're mapping a db column to another name
    $this->other_var_name = $value;
}
With this, the setter is called, but property it is setting is accessible, so the new value doesn't use the __set or __get function, and the changed array isn't updated with other_var_name as a changed property.
Is there some kind of hack, other that u开发者_运维知识库sing something like $this->set('variable', 'value') to achieve the result? I would just write getter's and setters, but they vary based on the db schema, and it would be lovely if there was an elegantly simple solution.
Try this,
$objects = array("model", "make", "version");
          foreach ($objects as $object) {
              $getter = "get".ucfirst($object);
              if (is_object($iProduct->$getter())) {
                  echo "getvalue"+$iProduct->$getter()
          }
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论